2014年5月27日 星期二

ASP.NET How to give the postbackurl in gridview link button

ASP.NET How to give the postbackurl in gridview link button


HTML:
<asp:GridView ID="GridView1" runat="server"  CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false">
                        <Columns>
                            <asp:TemplateField HeaderText="Model">
                                <ItemStyle HorizontalAlign="Center" Width="200" />
                                <ItemTemplate>
                                    <asp:LinkButton ID="lnkGvmodel" runat="server" Text='<%# Bind("model") %>' CommandArgument='<%# Eval("model") %>' PostBackUrl='<%# String.Format("~/ats/model.aspx?model={0}", Eval("model"))%>'></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                            </Columns>
</asp:GridView>

2014年5月24日 星期六

ASP.NET Combobox Sample

ASP.NET Combobox Sample
DropDownList TextBox


HTML:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style type="text/css">
        .t1
        {
            width:200px;
            position:absolute;
        }
        .d1
        {
            width:217px;
            height:22px;
            clip:rect(auto auto auto 200px);
            position:absolute;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" CssClass="t1" ></asp:TextBox>
        <asp:DropDownList ID="DropDownList1" runat="server" CssClass="d1" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"  AutoPostBack="true">
            <asp:ListItem Text="a" Value="1"></asp:ListItem>
            <asp:ListItem Text="b" Value="2"></asp:ListItem>
            <asp:ListItem Text="c" Value="3"></asp:ListItem>
        </asp:DropDownList>
    </div>
    </form>
</body>
</html>

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Test_tsCombobox : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TextBox1.Text = DropDownList1.SelectedItem.Text;
    }
}

2014年5月21日 星期三

ASP.NET GridView EditItemTemplate變換不同物件

ASP.NET GridView EditItemTemplate變換不同物件

在RowEditing時依據資料來源顯示不同的物件,下列以TextBox程DropDownList為例:
hfGvnetobject=>存放來自資料庫記載的物件別(TextBox or DropDownList)
hfGvnetobject.Value 等於TextBox時,顯示TextBox隱藏DropDownList物件
hfGvnetobject.Value 等於DropDownList時,顯示DropDownList隱藏TextBox物件
以下範例:

HTML:
<asp:TemplateField HeaderText="value">
    <ItemStyle HorizontalAlign="Center" Width="170" />
    <EditItemTemplate>
        <asp:HiddenField ID="hfGvnetobject" runat="server" Value='<%# Bind("netobject") %>' />
        <asp:TextBox ID="tbxGvvalue1" runat="server" Text='<%# Bind("value1") %>'></asp:TextBox>
        <asp:DropDownList ID="ddlGvvalue1" runat="server"></asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="lblGvvalue1" runat="server" Text='<%# Eval("value1") %>'></asp:Label>
    </ItemTemplate>
</asp:TemplateField>

C#:
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType != DataControlRowType.DataRow) // 非資料行, 離開
            return;
        if (e.Row.RowState == DataControlRowState.Edit || ((int)e.Row.RowState) == 5) //判斷為Edit模式,因偶數列無法判斷故加入((int)e.Row.RowState)==5的條件
        {
            HiddenField hfGvnetobject = e.Row.FindControl("hfGvnetobject") as HiddenField; //取得EditItemTemplate下的hfGvnetobject Value存放物件型態
            TextBox tbxGvvalue1 = e.Row.FindControl("tbxGvvalue1") as TextBox; //取得EditItemTemplate下的TextBox物件
            DropDownList ddlGvvalue1 = (DropDownList)e.Row.FindControl("ddlGvvalue1"); //取得EditItemTemplate下的DropDownList物件
            if (hfGvnetobject.Value == "TextBox") //假設物件型態等於TextBox, 顯示TextBox物件, 隱藏DropDownList物件
            {
                tbxGvvalue1.Visible = true;
                ddlGvvalue1.Visible = false;
            }
            if (hfGvnetobject.Value == "DropDownList") //假設物件型態等於DropDownList, 顯示DropDownList物件, 隱藏TextBox物件
            {
                tbxGvvalue1.Visible = false;
                ddlGvvalue1.Visible = true;
            }
        }
    }

2014年5月13日 星期二

ASP.NET取得觸發Postback的Button

ASP.NET取得觸發Postback的Button

HTML:
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" Width="100px" UseSubmitBehavior="false" />

C#:
protected void Page_Load(object sender, EventArgs e)
{
    if (this.Request.Form["__EVENTTARGET"] == btnSubmit.UniqueID)
    {
            btnSubmit.Enabled = false;
    }
}

ASP.NET防止等待回應中重複按Submit

ASP.NET防止等待回應中重複按Submit

Submit送出時改變Button Enable狀態,讓使用者無法再按Submit

HTML:
<asp:button id="btnSubmit" onclick="btnSubmit_Click" runat="server" text="Submit" usesubmitbehavior="false" width="100px">
</asp:button>

C#:
protected void Page_Load(object sender, EventArgs e)
{
    this.btnSubmit.Attributes.Add("onclick", ClientScript.GetPostBackEventReference(btnSubmit, "click") + ";this.disabled=true; this.value='Saveing...';");
}

2014年5月12日 星期一

ASP.NET 防止回上一頁重複新增

ASP.NET 防止回上一頁重複新增

----------------------------------------------------------------------------------------------------
.cs

protected void Page_Load(object sender, EventArgs e)
 {
      Response.Cache.SetCacheability(HttpCacheability.NoCache);
      Response.Cache.SetNoStore();            
      Response.Cache.SetExpires(DateTime.MinValue);
 }

 protected void Button1_Click(object sender, EventArgs e)
 {
       Save();
       Response.Redirect("success.aspx");
       //Server.Transfer("success.aspx", true);
 }

 private void Save()
 { }

2014年5月8日 星期四

ASP.NET Confirm dialog

ASP.NET Confirm dialog












Sample 1: ASP.NET
----------------------------------------------------------------------------------------------------
.aspx

<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" OnClientClick="return confirm('Do you really want to save?');" />


Sample 2: ASP.NET Call JAVASCRIPT Function
----------------------------------------------------------------------------------------------------
.aspx

<asp:Button runat="server" ID="btnUserDelete" Text="Delete" OnClick="BtnUserDelete_Click" OnClientClick="if ( ! UserDeleteConfirmation()) return false;"  />

<script type="text/javascript">
        function UserDeleteConfirmation() {
            return confirm("Are you sure you want to delete this user?");
        }
</script>


Sample 3: C#
----------------------------------------------------------------------------------------------------
.cs

protected void Page_Load(object sender, System.EventArgs e)
{
     if (!IsPostBack)
     {
         btnSave.Attributes["Onclick"] = "return confirm('Do you really want to save?');";        
     }
}