Reputation: 8695
I have a list of anchor tags that when clicked I would like the value of their ID attribute to be send to the parameter of a stored procedure. Everything works fine except I don't know how to make the second argument of the AddWithValue
method of the Parameters
property of the SqlCommand
accept whatever is clicked instead of hard coding it. The results are read from a simple stored procedure and then put into a grid view control
markup:
<%@ 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>
code behind
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();
}
}
}
}
}
Upvotes: 1
Views: 122
Reputation: 14608
I'm assuming these buttons are going to be created in a more generic fashion, e.g. within a repeater or something.
I would recommend changing the HyperLinks
to LinkButtons
so that you can access the OnClick
event and also putting the data access code into a method Then do something like: -
<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);
}
This answers the question as it was asked with regards to passing ID attribute as a parameter to the stored proc.
However, i would tweak this instead to use CommandArguments
like so:
<asp:LinkButton ID="Xtown" runat="server" OnClick="lnkButtonClick" CommandArgument="Xtown">Xtown</asp:LinkButton>
And access it in a similar way to the clientid:
protected void lnkButtonClick(object sender, EventArgs e)
{
string args = ((LinkButton)sender).CommandArgument;
UpdateDB(args);
}
Upvotes: 1
Reputation: 8920
Use the CommandArgument
of a LinkButton
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.commandargument.aspx
Upvotes: 2
Reputation: 2715
As of my understanding you should use LinkButton instead of hyperlink and handle the click event of each LinkButton. Put your SQL code in a separate parameterized method and pass the LinkButton Control Id from each event as an argument.
Upvotes: 0