I have a gridview in which I have manually generated a column for checkboxes as a HeaderTemplate as below
<asp:GridView ID="gvDB" runat="server" AutoGenerateColumns="False" DataKeyNames="Id" OnRowDataBound="gvDB_RowDataBound" <asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkSelectHeader" AutoPostBack="true" OnCheckedChanged="chkSelectHeader_CheckedChanged" runat="server"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" AutoPostBack="true" runat="server" OnCheckedChanged="chkSelect_CheckedChanged1" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And OnRowDataBound I'm dynamically generating controls and adding it to the each row
e.Row.Cells[rowIndex].Controls.Add(control);
And they are binding to the columns as expected.But my chkSelectHeader_CheckedChanged chkSelect_CheckedChanged1 events are not firing.
Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
AddTemplatesToGrid();
}
BindDataToGridView();
}
public void AddTemplatesToGrid()
{
DataTable dt = new DataTable();
foreach (Employees emp in EmployeesList)
{
TemplateField tfield = new TemplateField();
tfield.HeaderText = emp.Name;
gvDataEntry.Columns.Add(tfield);
}
}
You call BindDataToGridView on every postback which will discard events.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
AddTemplatesToGrid();
}
BindDataToGridView();
}
Include BindDataToGridView() in the !Page.IsPostBack-check.
Related
I have a scenario, where i need to attach an event (textchanged) to a textbox. It should only trigger, if the code is "code2". Please check the below code ; The value_TextChanged doesn't get triggered:
<asp:GridView runat="server" ID="gv1" AutoGenerateColumns="false"
onrowcreated="gv1_RowCreated1" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label runat="server" ID="code" Text='<%# Bind("[code]") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox runat="server" ID="val" Text='<%# Bind("[value]") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataBind();
}
}
void DataBind()
{
DataTable dt = new DataTable();
dt.TableName = "tb1";
dt.Columns.Add("code");
dt.Columns.Add("value");
dt.Rows.Add("code1", "Red");
dt.Rows.Add("code2", "Green");
dt.Rows.Add("code3", "Blue");
gv1.DataSource = dt;
gv1.DataBind();
}
protected void gv1_RowCreated1(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string code = (String)DataBinder.Eval(e.Row.DataItem, "code");
TextBox value = (TextBox)e.Row.FindControl("val");
if (code == "code2")
{
value.AutoPostBack = true;
value.TextChanged += new EventHandler(value_TextChanged);
}
}
}
void value_TextChanged(object sender, EventArgs e)
{
throw new NotImplementedException();
}
To trigger TextChange you need to add AutoPostBack="true" in your Markup; For attaching an event with TextChange you need not to depend gv1_RowCreated that also you can specify in the markup.
<asp:TextBox AutoPostBack="true" OnTextChanged="value_TextChanged"
runat="server" ID="val" Text="" ></asp:TextBox>
Please note one more thing, the event will trigger when the textBox
loss its focus
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 which contains a button in a template field. I want to change the button text when the button click event is completed. Can somebody send me a sample code.
Thanks in advance
Here is a sample bit of code using the RowCommand() of the GridView.
ASPX
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Label ID="lbl1" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false" CommandName="MYCOMMAND" Text="My Text!" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<string> lst = new List<string>() { "asd", "xxx" };
GridView1.DataSource = lst;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "MYCOMMAND")
{
Button Button1 = (Button)e.CommandSource;
if (Button1 != null)
Button1.Text = "changed text..";
}
}
I have binded a data grid to an array. Also, there is a button there to delete the row. The problem is that I am not sure how to implement it since the data source is an array.
See below
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:Label ID="lblItems" runat="server" Text='<%# Container.DataItem>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn ButtonType="PushButton" CommandName="Delete" Text="Delete">
</asp:ButtonColumn>
</Columns>
and here I would like to implement it..
private void DataGrid1_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
int rowToDelete = e.Item.ItemIndex;
myDataGrid.DataBind();
}
In the code for the deletion, how can I access the index of my array based on the button clicked (per row)?
Here is an example
Markup.
<asp:DataGrid ID="DataGrid1" runat="server"
AutoGenerateColumns="False"
OnDeleteCommand="DataGrid1_DeleteCommand">
<Columns>
<asp:TemplateColumn HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblItems" runat="server"
Text='<%# Container.DataItem %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:ButtonColumn ButtonType="PushButton"
CommandName="Delete"
HeaderText="Actions"
Text="Delete">
</asp:ButtonColumn>
</Columns>
</asp:DataGrid>
Code-behind.
private static string[] names = new string[] { "Matt", "Joanne", "Robert" };
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
private void BindGrid()
{
DataGrid1.DataSource = names;
DataGrid1.DataBind();
}
protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
{
string deletedItem = ((Label) DataGrid1.Items[e.Item.ItemIndex].FindControl("lblItems")).Text;
names = names.Where(val => val != deletedItem).ToArray();
BindGrid();
}
Hope this helps.
Have you tried to access your src object using the method below (notice this is 'RowDeleting' event on gridview)?
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
MyObject o = (MyObject)gv.Rows[e.RowIndex].DataItem;
}
There are also other tricks like storing an id in a hiddenfield on the row and then on the delete command you search for the control and pluck your value. My preference is the method above but really depends on what your goal is.
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
HiddenField field = (HiddenField)gv.Rows[e.RowIndex].FindControl("myHiddenField");
string myValue = field.Value;
// delete it and rebind
}
I stripped this example to make it simple. I have a gridview with a template field. The template field contains two buttons and a label (They must be in the same template field). I want the first button to set the label text to "Win", and the other button to set the label text to "fail". The onrowcommand doesnt seem to be triggered by buttons in a template field. How can I accomplish this?
My gridview code is below:
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True" AutoGenerateColumns="False"
OnRowCommand="Gridview1_RowCommand">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnWin" runat="server" CommandName="Win" Text="Win" />
<asp:Button ID="btnFail" runat="server" CommandName="Fail" Text="Fail" />
<asp:Label ID="lblStatus" runat="server" Text='<%# Bind("text") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my code behind:
protected void Page_Load(object sender, EventArgs e)
{
DataTable myTable = new DataTable();
myTable.Columns.Add("text", typeof(string));
myTable.Rows.Add("First Entry");
myTable.Rows.Add("Second Entry");
GridView1.DataSource = myTable;
GridView1.DataBind();
}
public void Gridview1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
//set lblStatus.text to "Win", or "Fail"
}
Thanks in advance!
Here you go...
public void Gridview1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
lblStatus.Text = e.CommandName;
}
I see that there is more to this question than is answered here, bear with me. One way would be to delegate the OnCommand event of each button to its designated event handler, as follows:
<div>
<asp:GridView ID="MyGridView" runat="server" EnableModelValidation="true" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="MyWinButton" runat="server" OnCommand="MyWinButton_OnCommand" CommandName="Win" Text="Win" />
<asp:Label ID="MyStatusLabel" runat="server" Text='<%# Bind("text") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
public void MyWinButton_OnCommand(Object sender, CommandEventArgs e)
{
var label = ((Button)sender).Parent.FindControl("MyStatusLabel") as Label;
label.Text = e.CommandName;
}
Also, as Alison suggests, you won't see the desired output of this unless you use !IsPostBack in Page_Load. Furthermore, on doing so this does in fact enable you to use the one row command event handler as initially suggested, albeit with a slight change in the label retrieval:
public void MyGridView_OnRowCommand(Object sender, GridViewCommandEventArgs e)
{
var label = ((Button)e.CommandSource).Parent.FindControl("MyStatusLabel") as Label;
label.Text = e.CommandName;
}
Are you using a MasterPage with ViewState turned off? Buttons in a template field with ViewState set to false will not fire.
Also, you should change your Page_Load to NOT rebind on postback"
protected void Page_Load(object sender, EventArgs e)
{
if (!page.IsPostBack) {
DataTable myTable = new DataTable();
myTable.Columns.Add("text", typeof(string));
myTable.Rows.Add("First Entry");
myTable.Rows.Add("Second Entry");
GridView1.DataSource = myTable;
GridView1.DataBind();
}
}
The rebinding may be interfering.