2013-04-09 39 views
1

我有點擊時,我想他們的ID屬性的值將被髮送到存儲過程的參數錨標記列表。一切正常,除非我不知道如何讓SqlCommandParameters屬性的AddWithValue方法的第二個參數接受任何被點擊的內容,而不是對它進行硬編碼。結果是從一個簡單的存儲過程讀取,然後放入網格視圖控制使用單擊元素的ID屬性作爲放慢參數

標記:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" Inherits="jQueryEachExample._Default" %> 

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent"> 
</asp:Content> 
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent"> 




    <asp:HyperLink ID="Xtown" runat="server">Xtown</asp:HyperLink> 
    <asp:HyperLink ID="Ztown" runat="server">Ztown</asp:HyperLink> 
    <asp:HyperLink ID="Atown" runat="server">Atown</asp:HyperLink> 
    <asp:HyperLink ID="Bburg" runat="server">Bburg</asp:HyperLink> 

<asp:GridView runat="server" ID="stateGridView"> 

</asp:GridView>  

</asp:Content> 

代碼背後

using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Configuration; 
using System.Data.SqlClient; 
using System.Data; 

namespace jQueryEachExample 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      //let the connection string be read from the web.config file 
      string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString; 

      //create a connection 
      using (SqlConnection conn = new SqlConnection(cs)) 
      { 
       //open connection 
       conn.Open(); 
       //create the SqlCommand object 
       SqlCommand cmd = new SqlCommand("spFindCityInfo", conn); 
       cmd.CommandType = CommandType.StoredProcedure; 
       //cmd.Parameters.AddWithValue("@cityName",this argument needs to be the id attribute of the link was that clicked 
       cmd.Parameters.AddWithValue("@cityName", Xtown.ID);//Xtown.ID is what needs to be whatever is clicked 
       //create a reader 
       using (SqlDataReader reader = cmd.ExecuteReader()) 
       { 
        stateGridView.DataSource = reader; 
        stateGridView.DataBind(); 
       } 


      } 


     } 
    } 
} 

回答

1

我假設這些按鈕會在一個更通用的方式被創建,例如,在一箇中繼器或其他東西。

我將建議改變HyperLinksLinkButtons這樣您就可以訪問OnClick事件,並把數據訪問代碼到一個方法然後做一些事情,如: -

<asp:LinkButton ID="Xtown" runat="server" OnClick="lnkButtonClick">Xtown</asp:LinkButton> 

private void UpdateDB(string id) 
{ 
    string cs = ConfigurationManager.ConnectionStrings["dbcs"].ConnectionString; 

    using (SqlConnection conn = new SqlConnection(cs)) 
    { 
     //*snip* 
     cmd.Parameters.AddWithValue("@cityName", id);//ID from clicked control 
    } 
} 


protected void lnkButtonClick(object sender, EventArgs e) 
{ 
    string id = ((LinkButton)sender).ClientID; 
    UpdateDB(id); 
} 

這個答案,因爲它是詢問關於傳遞ID屬性作爲存儲過程的參數。

不過,我想調整這個,而不是使用CommandArguments像這樣:

<asp:LinkButton ID="Xtown" runat="server" OnClick="lnkButtonClick" CommandArgument="Xtown">Xtown</asp:LinkButton> 

和訪問它以類似的方式來確定clientid:

protected void lnkButtonClick(object sender, EventArgs e) 
{ 
    string args = ((LinkButton)sender).CommandArgument; 
    UpdateDB(args); 
} 
0

由於我的理解,你應該使用,而不是超鏈接的LinkBut​​ton並處理每個LinkBut​​ton的點擊事件。將您的SQL代碼放入單獨的參數化方法中,並將每個事件的LinkBut​​ton控件ID作爲參數傳遞。

相關問題