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:
  1. <asp:GridView ID="GridView1" runat="server"  CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="false">
  2.                         <Columns>
  3.                             <asp:TemplateField HeaderText="Model">
  4.                                 <ItemStyle HorizontalAlign="Center" Width="200" />
  5.                                 <ItemTemplate>
  6.                                     <asp:LinkButton ID="lnkGvmodel" runat="server" Text='<%# Bind("model") %>' CommandArgument='<%# Eval("model") %>' PostBackUrl='<%# String.Format("~/ats/model.aspx?model={0}", Eval("model"))%>'></asp:LinkButton>
  7.                                 </ItemTemplate>
  8.                             </asp:TemplateField>
  9.                             </Columns>
  10.  
  11. </asp:GridView>

2014年5月24日 星期六

ASP.NET Combobox Sample

ASP.NET Combobox Sample
DropDownList TextBox


HTML:
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head runat="server">
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  4. <title></title>
  5. <style type="text/css">
  6. .t1
  7. {
  8. width:200px;
  9. position:absolute;
  10. }
  11. .d1
  12. {
  13. width:217px;
  14. height:22px;
  15. clip:rect(auto auto auto 200px);
  16. position:absolute;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <form id="form1" runat="server">
  22. <div>
  23. <asp:TextBox ID="TextBox1" runat="server" CssClass="t1" ></asp:TextBox>
  24. <asp:DropDownList ID="DropDownList1" runat="server" CssClass="d1" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="true">
  25. <asp:ListItem Text="a" Value="1"></asp:ListItem>
  26. <asp:ListItem Text="b" Value="2"></asp:ListItem>
  27. <asp:ListItem Text="c" Value="3"></asp:ListItem>
  28. </asp:DropDownList>
  29. </div>
  30. </form>
  31. </body>
  32. </html>

C#:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7.  
  8. public partial class Test_tsCombobox : System.Web.UI.Page
  9. {
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12.  
  13. }
  14. protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
  15. {
  16. TextBox1.Text = DropDownList1.SelectedItem.Text;
  17. }
  18. }

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:
  1. <asp:TemplateField HeaderText="value">
  2. <ItemStyle HorizontalAlign="Center" Width="170" />
  3. <EditItemTemplate>
  4. <asp:HiddenField ID="hfGvnetobject" runat="server" Value='<%# Bind("netobject") %>' />
  5. <asp:TextBox ID="tbxGvvalue1" runat="server" Text='<%# Bind("value1") %>'></asp:TextBox>
  6. <asp:DropDownList ID="ddlGvvalue1" runat="server"></asp:DropDownList>
  7. </EditItemTemplate>
  8. <ItemTemplate>
  9. <asp:Label ID="lblGvvalue1" runat="server" Text='<%# Eval("value1") %>'></asp:Label>
  10. </ItemTemplate>
  11. </asp:TemplateField>

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

2014年5月13日 星期二

ASP.NET取得觸發Postback的Button

ASP.NET取得觸發Postback的Button

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

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

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

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

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

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

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

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?');";        
     }
}