I have a GridView which is databound to an XML Datasource. For one of the columns I'm using a TemplateField and within it's ItemTemplate I have a CheckBox. I need to programatically add an EventHandler to the CheckBox. I was wondering if anyone can tell me which EventHandler from the GridView to use to add a CheckedChanged EventHandler to the CheckBox?
I've tried RowCreated and DataBound and haven't been able to get the CheckBox to postback with the CheckChanged EventHandler.
void gridPartnerSelection_RowCreated(object sender, GridViewRowEventArgs e)
{
GridViewRow row = e.Row;
if (row.RowType == DataControlRowType.DataRow)
{
CheckBox ckbSendEmail = row.Cells[2].FindControl("ckbSendEmail") as CheckBox;
ckbSendEmail.CheckedChanged += new EventHandler(ckbSendEmail_CheckedChanged);
}
}
Thank you.
Turns out I had to set the AutoPostBack property of the CheckBox to True :)
I'm willing to delete this question if it won't be helpful to anyone else.
Related
I'm registering a postback event for each row in an ASP.NET GridView.
protected void gvLRR_RowDataBound(object sender, GridViewRowEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
if (cs == null)
cs = Page.ClientScript;
e.Row.Attributes.Add("onclick",
cs.GetPostBackEventReference((GridView)sender, "Select$" + e.Row.RowIndex.ToString()));
}
}
The event that is called upon the user clicking a row is the SelectedIndexChanged of the GridView gvLRR.
PROBLEM:
All of this works just fine so long as I set the page directive EnableEventValidation to false, but if I do not set it to false then the page blows up when the user clicks on a row in the GridView. However, I see this as a bit of a hack, because I shouldn't have to disable event validation just to get postback events to work when clicking upon a GridView row. So is there a better way to go about doing this? Can I somehow register postback events for a row click and somehow manage to have event validation enabled still?
Thank you in advance for the help.
While searching the net, I found this ... click row on gridview and trigger postback c#
HTH :)
The row has a LinkButton that when clicked needs to highlight the row.
Code so far:
protected void linkbutton1_Click(object sender, EventArgs e)
{
LinkButton l = (LinkButton)sender;
GridViewRow g = (GridViewRow)l.Parent; // what is the correct way to do this?
//g.Style etc etc
}
first of all set the "CommandName" property of LinkButton to "select",
then in the selectedIndexChanging event of gridview write below code:
for (int i = 0; i < GridView1.Rows.Count;i++ )
GridView1.Rows[i].BackColor = System.Drawing.Color.White;
GridView1.Rows[e.NewSelectedIndex].BackColor = System.Drawing.Color.Cornsilk;
Make use of the RowCommand event of the GridView instead of the Click event of the LinkButton.
Then you can have a CommandName on the LinkButton such as "HighlightRow" and do something like the following:
Select Case e.CommandName
Case "HighlightRow"
e.item.row.attributes("class") = "highlight"
End Select
Sorry its in VB.NET and not C#
1.) Set Command Name Property to "Select"
2.) Change style either on code behind as shown by #Raymond or give set Cssclass attribute for
SelectedRowStyle of gridview to CssClass="selecterowstyle"
.selectedRowstyle
{
background-color:#EAEAEA;
}
I have a PlaceHolder control inside of a ListView that I am using to render controls from my code behind. The code below adds the controls:
TextBox tb = new TextBox();
tb.Text = quest.Value;
tb.ID = quest.ShortName.Replace(" ", "");
((PlaceHolder)e.Item.FindControl("ph_QuestionInput")).Controls.Add(tb);
I am using the following code to retrieve the values that have been entered into the TextBox:
foreach (ListViewDataItem di in lv_Questions.Items)
{
int QuestionId = Convert.ToInt32(((HiddenField)di.FindControl("hf_QuestionId")).Value);
Question quest = dc.Questions.Single(q => q.QuestionId == QuestionId);
TextBox tb = ((TextBox)di.FindControl(quest.ShortName.Replace(" ","")));
//tb is always null!
}
But it never finds the control. I've looked at the source code for the page and the control i want has the id:
ctl00_cphContentMiddle_lv_Questions_ctrl0_Numberofacres
For some reason when I look at the controls in the ListViewDataItem it has the ClientID:
ctl00_cphContentMiddle_lv_Questions_ctrl0_ctl00
Why would it be changing Numberofacres to ctl00? Is there any way to work around this?
UPDATE:
Just to clarify, I am databinding my ListView in the Page_Init event. I then create the controls in the ItemBound event for my ListView. But based on what #Womp and MSDN are saying the controls won't actually be created until after the Load event (which is after the Page_Init event) and therefore are not in ViewState? Does this sound correct?
If so am I just SOL when it comes to retrieving the values in my dynamic controls from my OnClick event?
UPDATE 2:
So i changed the code i had in my Page_Init event from:
protected void Page_Init(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//databind lv_Questions
}
}
to:
protected void Page_Init(object sender, EventArgs e)
{
//databind lv_Questions
}
And it fixed my problem. Still a little confused as to why I want to databind regardless of whether it's a postback or not but the issue is resolved.
It looks like you're adding your textbox to a Placeholder control... but then you're searching a ListViewDataItem container for it later.
Seems to me that you need to search for the Placeholder first, and then search it for the textbox.
How to access gridview commandfield delete button on RowDataBound event?
How the cells and controls in griview are accessed
Please try the code below. This is for adding a delete confirmation. But you can use it for anything you want.
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[1].HasControls())
{
LinkButton lnkbtnDelete = ((LinkButton)e.Row.Cells[1].Controls[0]);
lnkbtnDelete.Attributes.Add("onclick", "return confirm('Do you want to Delete?');");
}
}
HTH
See:
protected void YourGrid_RowDataBound(Object sender, GridViewRowEventArgs e)
{
Control button = e.Row.FindControl("btnSubmit");
if (button != null && checkBox is Button)
{
// do what you want
}
}
In RowDataBound event you may access row inner controls through FindControl method.
In the example above I assumed that you control is a Button control with btnSubmit identifier.
Edit: after the author's problem additional explanation:
(ButtonType)e.Row.Cells[commandFieldIndex].Controls[controlIndex];
ButtonType is the type of button being used by the CommandField - Button, LinkButton, or ImageButton. By default, the CommandField uses LinkButtons, but this can be customized via the CommandField’s ButtonType property.
I have 2 Gridviews which share the same OnSelectedIndexChanged event.
How do I get the incoming Gridview the one which fired so that I can pass that GridView to DetailsView. In that DetailsView I need to access the selected Gridview columns.
Thanks In Advance.
first parameter is "sender" as an object and you can cast it to a gridview object than check its ID.
GridView grd = (GridView) sender;
The "sender" parameter of the OnSelectedIndexChanged event should be the GridView that the event came from. You can get at it like this:
public void MyGrid_OnSelectedIndexChanged(object sender, EventArgs e)
{
GridView grid = sender as GridView;
if (grid != null)
{
// Do something with grid
}
}