I have a grid which contains a edit button . When i click the edit button and debug it does not hit to the selected index change event . There are no build errors
code behind the grid
public void btnModemDetailsEdit_Click(object sender, EventArgs e)
{
isEdit = true;
}
protected void gridModemDetails_SelectedIndexChanged(object sender, EventArgs e)
{
int id = Convert.ToInt32(GridModemDetails.DataKeys[GridModemDetails.SelectedIndex].Values["gridModemDetails_SelectedIndexChanged"].ToString());
}
<asp:GridView ID="GridModemDetails" runat="server" Width="435px"
DataKeyNames="ModemId" AllowPaging="True"
OnSelectedIndexChanged="gridModemDetails_SelectedIndexChanged"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Edit" Visible="True" >
<ItemTemplate>
<asp:LinkButton ID="btnModemDetailsEdit"
AccessibleHeaderText="Edit"
ButtonType="Button"
Text="Edit"
HeaderText="Edit"
runat="server"
OnClick="btnModemDetailsEdit_Click"/>
</ItemTemplate>
</asp:TemplateField>
The GridView's SelectedIndexChanged event is tied to the RowCommand event.
The simple way to get the SelectedIndexChanged event to fire is to use the AutoGenerateSelectButton property of the GridView, like this:
<asp:GridView AutoGenerateSelectButton="true"
This will add a button to each row with the text Select and when clicked, the SelectedIndexChange event will fire.
In the case of your edit button, you can use the CommandField in the grid view markup, like this:
<asp:GridView ...>
<Columns>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
Now in your code-behind, you can handle the RowCommand event, like this:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
// Get the actual row
GridViewRow theGridViewRow = GridView1.Rows(e.RowIndex);
// Do something with grid view row here
}
}
Related
I am having GridView control inside Wizard control, and I am having a linkbutton inside grid, clicking on which will change active index of wizard.
I have three GridViewControls and I am using same Event for RowCommand of these Grids, but its not working, I tried applying breakpoint but its not hitting the break point.
This is my code
w ID="GVUsers" runat="server" OnRowDataBound="GVUsers_RowDataBound" OnRowCommand="GVUsers_RowCommand"
OnRowDeleting="GVUsers_RowDeleting" AutoGenerateColumns="false" CssClass="table">
<Columns>
<asp:TemplateField HeaderText="Crimes" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="Username" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Username") %>'></asp:Label>
<asp:Label ID="gender" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Gender") %>'></asp:Label>
<asp:Panel ID="divmsg" runat="server">
<asp:LinkButton
ID="btnlnkpg18" runat="server" Text="Click here" CommandName="pg18"></asp:LinkButton>
</asp:Panel>
</ItemTemplate>
<asp:TemplateField>
<asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:LinkButton ID="delbtn" runat="server" Text="Delete" OnClientClick="return confirm('Do you really want to delete?');"
CommandName="delete" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "UserId") %>'
CssClass="DeleteBtn"></asp:LinkButton>
</ItemTemplate>
<asp:TemplateField>
</Columns>
</asp:GridView>
protected void GVUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;
var lblgender= e.Row.FindControl("gender") as Label;
var divlnk=e.Row.FindControl("divmsg") as Panel;
if(lblgender.Text=="M")
divlink.Visible=true;
else
divlink.Visible=false;
}
}
protected void GVUsers_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "delete")
{
//Delete
}
if (e.CommandName == "pg18")
{
Wizard1.ActiveStepIndex = 16;
}
}
I also tried setting CommandName in RowDataBound but no luck, Also the Delete Button is not working.
I am databinding GridView like this
if(!Page.IsPostBack)
{
//Bind GridView
}
Syed, try doing this:
1) Rename your RowDataBound event to this:
protected void temp()
2) Go to the aspx page and delete the RowDataBound property from the gridview.
3) With the gridview still highlighted, click design view.
4) In properties click on 'Events' (the lightning bolt).
4) Double click inside the box for RowDatabound to create the event handler in the .cs
5) With in the RowDatabound inside the .cs add a reference to temp();
6) Add a breakpoint just about the temp() within RowDatabound and run your code.
See if it hits the code, if it does then copy everything from test into the RowDataBound.
I have a gridview row that when clicked has to do postback A and a buttonfield in that row that when clicked has to do postback B. The problem is that when i click on the buttonfield, both event1 and event2 gets fired. Below is the code.
protected void gdv_RowCommand(object sender, GridViewCommandEventArgs e)
{
string arg = Convert.ToString(((System.Web.UI.WebControls.CommandEventArgs)(e)).CommandArgument);
if (e.CommandName == "Command1")
{
doEvent1(arg);
}
else if (e.CommandName == "Command2")
{
doEvent2(arg);
}
}
protected void gdv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton b1 = (LinkButton)e.Row.Cells[0].Controls[0];
matchesButton.CommandArgument = arg1;
LinkButton rowLink = (LinkButton)e.Row.Cells[1].Controls[1];
rowLink.CommandArgument = arg2;
e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(rowLink, "");
}
}
And this is the asp code for the gridview
<Columns>
<asp:ButtonField DataTextField="f1" HeaderText="H1" CommandName="Command1" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btn1" runat="server" Text="" CommandName="Command2" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
try to use this
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Command2")
{
// Your Code here
}
}
to find control in grid view use this code
LinkButton lnkbtn= (LinkButton)e.Row.FindControl("btn1");
Try adding both the button with in same <asp:TemplateField> if you don't want separate headers
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="button" runat="server" CommandName="Command1" />
<asp:LinkButton ID="btn1" runat="server" CommandName="Command2" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
if you want separate headers make two separate <asp:TemplateField> and then add buttons in them.
I have a grid view with a SelectedIndexChanged event that fires when the user selects a row.
Now in the grid view row, I add a drop down list using TemplateField, and in the grid view RowDataBound event I add code for firing the SelectedIndexChanged event of the drop down list.
So, when user click on drop down list the first event that fires is the SelectedIndexChanged of the grid view, then page goes into post back and the selection of drop down list is lost.
The SelectedIndexChange of drop down list fires only if user is faster on changing selection than page goes into post back.
I need that when user select a row with drop down list grid view's event wait for the drop down list selection and after first event that can fire is drop down list changing and at last grid view's SelectedIndexChanged event.
Is this possible?
some code :
<asp:GridView ID="grEventi" runat="server" BackColor="White"
ShowHeaderWhenEmpty="True" AutoGenerateColumns="False"
BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
CellPadding="3" Width="100%" OnRowDataBound="grEventi_RowDataBound"
onselectedindexchanged="grEventi_SelectedIndexChanged" >
<SelectedRowStyle CssClass="selectedRow" />
<Columns>
<asp:BoundField DataField="Elenco Eventi" HeaderText="Evento" />
<asp:TemplateField ItemStyle-Wrap="false" ItemStyle-Width="150" HeaderText="Data Inizio">
<ItemTemplate>
<asp:Label ID="lbl_data" runat="server" Text="" Visible="false" >
</asp:Label>
<asp:DropDownList ID="ddl_data" runat="server" Visible="false" OnSelectedIndexChanged="ddl_dat_SelectedIndexChanged" ClientIDMode = "Static" class ="calendar">
</asp:DropDownList>
</asp:TemplateField>
</Columns>
</asp:GridView>
code-behind:
protected void ddl_dat_SelectedIndexChanged(object sender, EventArgs e)
{
//your logic goes here
string test = "";
}
protected void grEventi_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", this.ClientScript.GetPostBackEventReference((Control)sender, "Select$" + e.Row.RowIndex));
DropDownList ddl_dat = (DropDownList)e.Row.FindControl("ddl_data");
ddl_dat.SelectedValue = DataBinder.Eval(e.Row.DataItem, "data inizio").ToString();
ddl_dat.Visible = true;
ddl_dat.DataTextFormatString = "{0: ddd d/MM/yyyy HH:mm}";
ddl_dat.DataTextField = "data inizio";
ddl_dat.DataValueField = "data inizio";
ddl_dat.DataSource = mydata;
// ddl_dat.AutoPostBack = true;
ddl_dat.DataBind();
}
else
{
Label lbl_data = (Label)e.Row.FindControl("lbl_data");
lbl_data.Visible = true;
DateTime date=(DateTime)dr.Row["data inizio"];
lbl_data.Text = date.ToString("ddd d/MM/yyyy HH:mm");
}
}
protected void grEventi_SelectedIndexChanged(object sender, EventArgs e)
{
//my logic code
}
I want to access an object (hyperlink or button) within a templatefield in a gridview.
How do I do this?
Assuming that you're binding data to it, you'll want to look at doing that inside the RowDataBound event.
Here's an example of how to retrieve a control within a Template Field:
.aspx:
<asp:GridView ID="GridView1" Runat="server"
OnRowDataBound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Template Field">
<ItemTemplate>
<asp:Button ID="btnTest" Runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-behind:
void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Button btnTest = (Button)e.Row.FindControl("btnTest");
btnTest.Text = "I'm in a Template Field";
}
}
You can use it on RowDataBound or click event of your template control like
TextBox txtTemp= (TextBox )e.Row[e.RowIndex].FindControl("yourControlName");
string someText=txtTemp.Text;
I have the following GridView
<asp:GridView ID="grdImoveis" runat="server" DataSourceID="dsGrid">
<Columns>
<asp:BoundField HeaderText="Nome" DataField="NomeCompleto" />
<asp:ImageButton ID="ibtnAlterar" ImageUrl="../tema/_Internas/icons/edit.png" runat="server" OnClick="btnChange_Click" />
</Columns>
</GridView>
How can i get the value of this field in code behind and pass the value to my btnChange_Click event ?
If your asking what I think, you'd like want to handle the OnRowCommand of your GridView and capture the button action in there.
<asp:GridView ID="grdImoveis" onrowcommand="grdImoveis_RowCommand" ...
Code-Behind:
protected void grdImoveis_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "BUTTON")
{
// Check value of e.CommandArgument and do something here:
}
}
And change your imagebutton to something like so by setting CommandName and CommandArgument to the value you want to pass. You also have to wrap it in a TemplateField:
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton CommandName="BUTTON" CommandArgument='<%#Eval("NomeCompleto") %>' ID="ibtnAlterar" ImageUrl="../tema/_Internas/icons/edit.png" runat="server" />
</ItemTemplate></asp:TemplateField>
Use CommandArgument property like this
CommandArgument='<%# Container.DataItemIndex %>'
inside a TemplateField and then in your code behind 'OnRowCommand' event handler use it like this:
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
object dataItem = gv.Rows[int.Parse(e.CommandArgument.ToString())].DataItem;
}