我有點擊時,我想他們的ID屬性的值將被髮送到存儲過程的參數錨標記列表。一切正常,除非我不知道如何讓SqlCommand
的Parameters
屬性的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();
}
}
}
}
}