使用单击元素的ID属性作为参数

时间:2013-04-09 14:40:14

标签: c# asp.net visual-studio-2010

我有一个锚标记列表,点击后我希望将其ID属性的值发送到存储过程的参数。一切正常,但我不知道如何使AddWithValue Parameters属性的SqlCommand方法的第二个参数接受点击而不是硬编码。结果从简单的存储过程中读取,然后放入网格视图控件

标记:

<%@ 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();
                }


            }


        }
    }
}

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:1)

我假设这些按钮将以更通用的方式创建,例如在转发器或其他东西。

我建议您将HyperLinks更改为LinkButtons,以便您可以访问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);
}

答案 2 :(得分:0)

据我所知,你应该使用LinkBut​​ton而不是超链接来处理每个LinkBut​​ton的click事件。将SQL代码放在单独的参数化方法中,并将每个事件的LinkBut​​ton Control Id作为参数传递。