我有一个锚标记列表,单击时我希望将其 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();
}
}
}
}
}