this is my gridview code
<
asp:GridView ID="dgAssociatedAttributes" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Width="80%"> <FooterStyle BackColor="#CCCCCC" ForeColor="Black" /> <RowStyle BackColor="#EEEEEE" ForeColor="Black" /> <Columns> <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="60%"/> <asp:TemplateField HeaderText="Is Required" ItemStyle-HorizontalAlign="Center" ItemStyle-Width="25%" > <ItemTemplate> <asp:CheckBox ID="chkIsRequired" runat="server" Checked='<%# Eval("IsRequired") %>' AutoPostBack="true" OnCheckedChanged="IsRequired_Click" /> </ItemTemplate> </asp:TemplateField> <asp:TemplateField ItemStyle-Width="39%"> <ItemTemplate > <asp:LinkButton ID="lbDelete" runat="server" CausesValidation="false" CommandArgument='<%# Eval("AttributeId") %>' OnCommand="lbDelete_Click" >Delete</asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="#DCDCDC" /></asp:GridView> i want that when i check or uncheck the checkbox it fire a event protected void IsRequired_Click(object sender, EventArgs e){
productId = Convert.ToInt32(Request.QueryString["productId"]); CheckBox checkbox = sender as CheckBox;if (checkbox != null){
GridViewRow row = checkbox.NamingContainer as GridViewRow; int attributeId = 0;if (row != null){
int.TryParse(dgAssociatedAttributes.DataKeys[row.RowIndex].ToString(), out attributeId);}
if (checkbox.Checked){
db.updateIsRequired(productId, attributeId, true);}
else{
db.updateIsRequired(productId, attributeId, false);}
LoadAssociatedAttributes();
}
}
in this code i simply insert the checked value in database through store procedure
but problem is that i am not able to get the value of "attributeid" . i thought in "EventArgs e" i will b able to get the value like i get it with
"e.CommandArgument" in delete button click event . but here "e" is returning null
now how can i get the value of attribute id (the id of row that checkbox is clicked).
plz help me out on this .thanks in advance
Ahtesham
![]() |
0 |
![]() |
You can easily retrieve the primary key of your data as you are currently doing, but no where in your GridView declaration do I see that you have the DataKeyNames property defined. You must first add the name of your primary key field to this property if you expect to receive a value out of it later.
Thanks, Ed
Microsoft MVP - ASP/ASP.NET
![]() |
0 |
![]() |
Make the following changes.
1. Add an invisible label to that gridview itemtemplate
<ItemTemplate> <asp:CheckBox ID="chkIsRequired" runat="server" Checked='<%# Eval("IsRequired") %>' AutoPostBack="true" OnCheckedChanged="IsRequired_Click" /><asp:Label ID="lblId" runat="server" Visible="false" Text='<%# Bind("TableId")' />
</ItemTemplate>2. Now in checkbox changed event,
CheckBox chk = (CheckBox)sender; GridViewRow gvRow = (GridViewRow)chk.NamingContainer;Label lblId = (Label)gvRow.FindControl("lblId"); string tableid = lblId.Text;I think you are vb.net programmer. understand the point and make in c#. Use Covnersion tools
labs.developerfusion.co.uk/convert/csharp-to-vb.aspx
Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
![]() |
0 |
![]() |
<script runat="server">
String mac; // This is used to store the TableCell 'userid' within the Gridview
//String hostName; // This is used to store the TableCell 'Host Name' within the Gridview
String activity; // This is used to store the TableCell 'Activity within the Gridveiw
String buildingNo; // This is used to store the TableCell 'Building# within the Gridveiw
String description2; // This is stored in the table 'user1' decription.
String NASEuser; // This is used to store UserName to retrieve email form database
String userEmail; // This is used to store the user's Emailprotected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
// This will assign the String variables their values pulling them from
// the gridview.
if (e.CommandName == "Update")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = GridView1.Rows[index];
TableCell userid = selectedRow.Cells[1];
mac = userid.Text;
TableCell BuildingNo = selectedRow.Cells[2];
buildingNo = BuildingNo.Text;
//TableCell ComputerName = selectedRow.Cells[5];
//hostName = ComputerName.Text;
TableCell ActivityName = selectedRow.Cells[3];
activity = ActivityName.Text;
description2 = activity + " BLDG " + buildingNo; //This is inserted into the description column
//within the 'user1' table
TableCell UserName = selectedRow.Cells[6];
NASEuser = UserName.Text;
}
}protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
// First I have to read the table 'macreg' to verify that the 'SqlDataSourceGrid' inserted true or false
// within the table 'macreg', column 'MACcomplete'. If the checkbox within the gridview is checked, then
// enabled will be true, else it will be false.
try
{
String enabled;
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["cs"].ToString()))
{
cn.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO [user1] ([userid], [passwd], [description]) VALUES (@userid, @passwd, @description)", cn);
SqlCommand cmd2 = new SqlCommand("SELECT * FROM macreg WHERE userid ='" + mac + "'", cn);
SqlDataReader rdr = cmd2.ExecuteReader();
rdr.Read();
enabled = rdr["MACcomplete"].ToString();
rdr.Close();
if (enabled == "True")
{
//Parameters
cmd.Parameters.Add("@userid", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@passwd", SqlDbType.VarChar, 64);
cmd.Parameters.Add("@description", SqlDbType.NVarChar, 512);//Values
cmd.Parameters["@userid"].Value = mac;
cmd.Parameters["@passwd"].Value = mac;
cmd.Parameters["@description"].Value = description2;
cmd.ExecuteNonQuery();
}
cn.Close();
}
}
catch (Exception err)
{
ErrorMessageLabel.Text = "ERROR OCCURRED ";
ErrorMessageLabel.Text += err.Message;
Response.Write(err);
}
}</script>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSourceGrid"
CellPadding="4" DataKeyNames="userid" ForeColor="#3B3A60" GridLines="None" PageSize="25" Font-Names="Palatino Linotype"
BorderColor="#504E6C" BorderStyle="Solid" BorderWidth="1px" HorizontalAlign="Center" Width="100%" OnRowUpdated="GridView1_RowUpdated" OnRowCommand="GridView1_RowCommand" Font-Size="Small" AllowPaging="True" AllowSorting="True">
<FooterStyle BackColor="#2A203B" Font-Bold="True" ForeColor="White" BorderColor="#2A203B" BorderStyle="Solid" BorderWidth="10px" />
<Columns>
<asp:CommandField ShowEditButton="True" EditText="Complete" >
<ItemStyle ForeColor="#FF8000" />
</asp:CommandField>
<asp:BoundField DataField="userid" HeaderText="MAC" ReadOnly="True" SortExpression="userid" />
<asp:BoundField DataField="BuildingNo" HeaderText="Building" ReadOnly="True" SortExpression="BuildingNo" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="ActivityName" HeaderText="Activity" ReadOnly="True" SortExpression="ActivityName" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="Device" HeaderText="Device" ReadOnly="True" SortExpression="Device" visible="False" />
<asp:BoundField DataField="ComputerName" HeaderText="ComputerName" ReadOnly="True"
SortExpression="ComputerName" >
<ItemStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="UserName" HeaderText="User" ReadOnly="True" SortExpression="UserName" />
<asp:BoundField DataField="DateRequested" HeaderText="Date Requested" SortExpression="DateRequested" DataFormatString="{0:g}" ReadOnly="True" />
<asp:CheckBoxField DataField="MACcomplete" HeaderText="Completed" SortExpression="MACcomplete" />
</Columns>
<SelectedRowStyle BackColor="#FFE0C0" ForeColor="#2A203B" Font-Bold="True" BorderColor="#2A203B" BorderStyle="Solid" BorderWidth="10px" Font-Size="Small" />
<PagerStyle ForeColor="White" HorizontalAlign="Center" BorderColor="#2A203B" BorderStyle="Solid" BorderWidth="10px" />
<HeaderStyle BackColor="#2A203B" Font-Bold="True" ForeColor="White" BorderColor="#2A203B" BorderStyle="Solid" BorderWidth="1px" HorizontalAlign="Center" />
<AlternatingRowStyle BackColor="#EDEDED" ForeColor="#2A203B" BorderColor="#2A203B" BorderStyle="Solid" BorderWidth="10px" />
<RowStyle BackColor="White" ForeColor="#2A203B" BorderColor="#2A203B" BorderStyle="Solid" BorderWidth="10px" />
<EditRowStyle BorderStyle="Solid" BorderWidth="10px" HorizontalAlign="Center" Font-Bold="True" Font-Overline="False" Font-Size="Small" Font-Underline="True" />
<EmptyDataRowStyle BorderColor="#2A203B" BorderStyle="Solid" BorderWidth="10px" HorizontalAlign="Center" />
<EmptyDataTemplate>
<font color="red">No Open Work Orders</font>
</EmptyDataTemplate>
</asp:GridView><asp:SqlDataSource ID="SqlDataSourceGrid" runat="server" ConnectionString="<%$ ConnectionStrings:SBRcs %>"
SelectCommand="SELECT userid, BuildingNo, ActivityName, Device, ComputerName, SUBSTRING(UserName, 6, 20) AS UserName, MACcomplete, DateRequested FROM macreg WHERE (MACcomplete = @MACcomplete)"
UpdateCommand="UPDATE macreg SET MACcomplete = @MACcomplete ,DateCompleted = getdate() WHERE (userid = @userid )">
<SelectParameters>
<asp:Parameter DefaultValue="False" Name="MACcomplete" Type="Boolean" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="MACcomplete" />
<asp:Parameter Name="userid" />
</UpdateParameters>
</asp:SqlDataSource>
~~ Believe in Science, not religion. Science will always progress, religions will always collapse. ~~
![]() |
0 |
![]() |
You can try this way.
fire a JavaScript function OnClick event of the checkBox. (eg: OnClick = "test(this);")
and in that function u wil b able to write like this.
function test(obj)
{
document.getElementById('HiddenField').value = obj.parentElement.parentElement.children[3].children[1].children[0].innerText;
}
you can place a debugger and check for the exact value in trial and error method.
HTH
Muppidi.
~ Muppidi
![]() |
0 |
![]() |
If the DataKeys aren't being defined for whatever reason, Why not just add an attribute to the checkbox itself?
<ItemTemplate> <asp:CheckBox ID="chkIsRequired" runat="server" Checked='<%# Eval("IsRequired") %>' MyKey='<%# Eval("IsRequired") %>AutoPostBack="true" OnCheckedChanged="IsRequired_Click" /> </ItemTemplate>
Then, in code, you could reference:
chk = e.FindControl("chkIsRequired")
myVal = chk.Attributes["MyKey"].ToString();
I've left out a lot of code, but hopefully it is clear what I am saying.
![]() |
0 |
![]() |
ecbruck i did define as u said but still i was not able to get the required id
and thanks every one for sharing there code and try to help me but they are more complex for my knowledge i tried the ramireddyindia way and it did work .
Ahtesham
![]() |
0 |
![]() |