get the Product SKU Of the row which the button pressed
protected void On_Update_Click(object sender, EventArgs e)
{
}
The easiest way is to use the DataKeyNames property.
<asp:GridView ID="GridView1" runat="server" DataKeyNames="SKU">
And then you can access the correct DataKeyNames based on the Row Index.
protected void On_Update_Click(object sender, EventArgs e)
{
//cast the sender back to a control and get the gridviewrow from the namingconainer
GridViewRow row = (GridViewRow)(((Control)sender).NamingContainer);
//get the gridview itself from the gridviewrow
GridView gv = row.Parent.Parent as GridView;
//get the correct datakey from the gridview with the dataitemindex
int SKU = Convert.ToInt32(gv.DataKeys[row.DataItemIndex].Values[0]);
//display result
Label1.Text = SKU.ToString();
}
Related
I would like to get the value and id of a checkbox in a repeater, and save the value in database.
Here is what I have;
Repeater;
I have an id like this ; <%# DataBinder.Eval(Container.DataItem, "ID")%>">
the checkbox i want to use
<asp:CheckBox ID="chkRemind" runat="server" OnCheckedChanged="Check_Changed" AutoPostBack="true" />
code behind
protected void Check_Changed(Object sender, EventArgs e) {
I need to know the Id and find out the current checkbox value
so i can do
UpdateDB(ID, checkboxValue);
}
You can databind the id to a data- attribute, then read that on postback.
cb.Attributes["data-id"] = DataItem.ID;
protected void Check_Changed(Object sender, EventArgs e) {
var checkbox = sender as CheckBox;
int ID = int.Parse(checkbox.Attributes["data-id"]);
UpdateDB(ID, CheckBox.Checked);
}
Make sure you verify that the current user has permission to edit the requested item, since the data-id value can be changed on the client.
Do this:
protected void Check_Changed(Object sender, EventArgs e) {
var item = sender as Checkbox;
UpdateDB(item.ID, item.Value);
}
Variable sender contains Control, that triggered event, so you need just cast it to CheckBox
protected void Check_Changed(Object sender, EventArgs e)
{
CheckBox chbSomeCheckbox = (CheckBox)sender;
var ID = chbSomeCheckbox.ID;
var checkboxValue = chbSomeCheckbox.Checked;
UpdateDB(ID, checkboxValue);
}
All you have to do is get sender control's ID
protected void Check_Changed(Object sender, EventArgs e)
{
Checkbox item = sender as Checkbox;
UpdateDB(item.ID, item.Value);
}
I try to delete a record in Gridview and Database .I added a boundfield button and set the CommandName to Deleterecord in the RowCommand event I want to get the primary key of this record to delete that(primary key value does not show in the grid view. The following block of code shows this event(I try to show some data in a text box):
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Deleterecord")
{
TextBox1.Text = GridView1.DataKeys[GridView1.SelectedIndex].Value.ToString();
//bla bla
}
}
I also set
DataKeyName="sourceName"
in the gridview but it is not my primary key
If I click this button an exception occured like this:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
How can I solve this problem
Use this
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Deleterecord")
{
LinkButton lb = (LinkButton)e.CommandSource;
GridViewRow gvr = (GridViewRow)lb.NamingContainer;
TextBox1.Text = grid.DataKeys[gvr.RowIndex].Value.ToString();
//bla bla
}
}
Here LinkButton should be replaced by the type of control which is performing this RowCommand Event
For more info go Here
I am using two dropdownlist in gridview. Total 5 to 7 rows in gridview.
Bind ProductId and product name in First Dropdown. when select product that time databound in second dropdown.
so how to findout row number when fire dropdown's event.
I assume the property you're looking for is GridViewRow.RowIndex. To get the reference to the row from the SelectedIndexChanged event from your DropDownLis(s), you can use it's NamingContainer property:
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
int rowIndex = row.RowIndex;
// if you want to get the reference to the other DropDown in this row
DropDownList ddl2 = (DropDownList)row.FindControl("DropDownList2");
}
You can handle SelectedIndexChanged event of DropDownList.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow gvr = (GridViewRow)((DropDownList)sender).Parent.Parent;
string rowNumber= GridView1.DataKeys[gvr.RowIndex].Value.ToString();
}
i need to get the value of the selected row of radiobutton in the gridview. the selected value is assigned to a textbox. my code is
protected void lnBTNDone_Click(object sender, EventArgs e)
{
GridViewRow gvRow = grdEventDetails.SelectedRow;
txtEventId.Text = gvRow.Cells[0].Text;
}
the problem is the gvRow value is assigned as null .
the linkbutton is not inside gridview. the gridview n linkbutton and the textbox are inside the user controls.
You can always get the GridViewRow via the sender's NamingContainer:
protected void lnBTNDone_Click(object sender, EventArgs e)
{
LinkButton lnBTNDone = (LinkButton)sender;
GridViewRow row = (GridViewRow)lnBTNDone.NamingContainer;
txtEventId.Text = row.Cells[0].Text;
}
Assuming that the LinkButton is in the selected row.
Btw, i'm yet not sure why SelectedRow is null there, maybe because the GridView's SelectedIndexChanged event is triggered after the LinkButton's click event.
protected void lnBTNDone_Click(object sender, EventArgs e)
{
for (int i = 0; i < grdEventDetails.Rows.Count; i++)
{
RadioButton rb = (grdEventDetails.Rows[i].FindControl("grdRdo")) as RadioButton;
if (rb.Checked == true)
{
txtEventId.Text = grdEventDetails.Rows[i].Cells[1].Text;
}
}
}
I have a dropdownlist in my Gridview and I am binding a datasource to the gridview.
Though all the records are displaying properly the dropdown value is not selected.
How do I set something like
<%# Bind("Country") %> for a dropdownlist in the Gridview in ASP.net.
Thanks
You can hook into the RowDataBound event for the grid view, find the control and set the value.
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
var dropdownList = e.Row.FindControl("YOUR_DROP_DOWN") as DropDownList;
dropdownList .SelectedIndex = SET_VALUE_HERE;
}
Setting DropDownList value from a Datasource should be like:
protected void gridview_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlCountry = (DropDownList)e.Row.FindControl("ddlCountry");
ddlCountry.SelectedValue = DataBinder.Eval(e.Row.DataItem, "Country").ToString();
}
}