C# GridView / RadioButton is not fireing an event... why? - c#

I have a gridview with rows of data and a group of radio buttons for each row that can be selected. But when selected the event never fires for the radio dropdown.... why?
<ItemTemplate>
<asp:RadioButton ID="rdoV"
runat="server" Checked='<%#
Bind("VOID") %>' AutoPostBack="True"
oncheckedchanged="RadioButton1_CheckedChanged"
GroupName='<%# BIND("WallstreetID")
%>' />
</ItemTemplate>
protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
}

Make sure you're not re-databinding the grid in the Page_Load event without checking for IsPostBack. That could wipe out your other events internal to the grid.

Related

How to fire Image Button click event in infragistics webdatagrid in C#?

I am using Infragistics (v. 12.2) WebDataGrid in which I have TemplateFieldColumn with Image button.
I want to capture the click event of this Image button. How to achieve that?
I have below code sample –
<asp:Panel runat=”server”>
<ig:WebDataGrid ID=”sampleWebGrid” runat=”server” AutoGenerateColumns=”False” Width=”100%”>
<Columns>
<ig:TemplateDataField Key=”keyBtnApply” Header-Text=”update”>
<ItemTemplate>
<asp:ImageButton ID=”btnUpdate” runat=”server” CommandName=”U” Width=”30px” Height=”30px” CommandArgument=”args” ImageUrl=”~/Images/Blue-Button-Icon.png”>
</ItemTemplate>
< /ig:TemplateDataField>
</Columns>
</ig:WebDataGrid>
</asp:Panel>
You can attach to the event from the markup like:
asp:ImageButton ID="btnUpdate" runat="server" OnClick="imageBtn_Click"
or define it in Page_Load event like:
ImageButton imageBtn = (ImageButton)sampleWebGrid.Rows[0].Items[0].FindControl("btnUpdate");
imageBtn.Click += imageBtn_Click;
void imageBtn_Click(object sender, ImageClickEventArgs e)
{
}
However you need to loop though all rows to find the control on the given row if you go with this approach

How to set gridview to edit mode on button click instead of using commandname

Hi all I am having a template field as follows with an itemtemplate
<asp:TemplateField HeaderText="Edit/Delete">
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" Text="Edit" OnClick=lnkEdit_Click"> </asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Generally instead of Click event we use to write CommandName="Edit" and on OnRowEditing event we will set gridview row to edit mode with the following code
protected void grdDemo_RowEditing(object sender, GridViewEditEventArgs e)
{
grdDemo.EditIndex = e.NewEditIndex;
bindGrid();
}
Instead of this I would to set gridview row to edit mode on link button click, how can we do that any ideas please
There are couple of other option available since you wish to ignore the commandname :)
Click anywhere to activate edit mode in gridview
Activate Edit mode based on ID - Datakey
Set the EditIndex property to the appropriate row and then ReBind the GridVIew again to it's DataSource.
protected void btnEdit_Click(object sender, EventArgs e)
{
GridView1.EditIndex = 1;
}
Google/Bing for more..
You can use edit item template as follows
the following is the sample aspx code
<ItemTemplate>
<asp:LinkButton ID="lblSubject" Width="100%" Height="100%" CommandName="Edit" ForeColor="Black" runat="server" Text='<%#Bind("Subject") %>'>
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="lblSubject" runat="server" Text='<%#Bind("Subject") %>'>
</asp:TextBox>
</EditItemTemplate>

redirecting to a url from a templatefeild in grid view

I have a grid view in my page in which i added a template feild.inserted a button which upon clicking will redirect the user to the actual request to make edits. The button that i added in the item template is bound to the data.The below is the code for the template feild
<asp:TemplateField HeaderText="RequestId" SortExpression="roc_id">
<EditItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Bind("roc_id") %>'></asp:HyperLink>
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text='<%# Bind("roc_id") %>' />
</ItemTemplate>
</asp:TemplateField>
how do i read the text from the botton click and redirect the user to the request that they click.I have listed the url below and i need to read the requestid at the end of url
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("http://xxxxxxxxxxxxxxxxxxxxxxxxxxx/Edit.asp?ROCID=
}
Thanks
You can use GridView.RowCommand to capture the button click event.
Also, you need to add a command name to the button in template field.
There is a good article about Respond to Button Events in a GridView Control which will help you

Databind fires after ItemUpdated event

I'm using an asp.net 4 web site project (C#) in VS2008 and I have a FormView with ItemUpdated event:
<asp:FormView ID="FormView1" runat="server" DataSourceID="ds1" OnItemUpdated="FormView1_ItemUpdated">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("col1") %>' />
</EditItemTemplate>
</asp:FormView>
protected void FormView1_ItemUpdated(object sender, EventArgs e)
{
FormView1.DataBind(); // adding this line even doesn't help
TextBox box = FormView1.FindControl("TextBox1") as TextBox;
box.Enabled = false;
}
But I can't figure out, why an extra "FormView1.DataBind()" or Render(?) happens AFTER the ItemUpdated event. The result is that my code in the ItemUpdated event gets like "overwritten" and the TextBox1 doesn't get disabled.
When I set a breakpoint to the last line "box.Enabled = false;" then I see that after the ItemUpdated event it jumps to the aspx page again and steps through the TextBoxes.
Disabling this TextBox1 from another GridView1_SelectedIndexChanged works fine.
Is there any way to see the "current lifecycle progress" in debugging?
EDIT:
To clarify the reasoning...
I have a GridView1 where selecting the item populates the abovementioned FormView1. The point is that I need to disable some of the TextBoxes in the FormView1 based on, for example, user access levels.
Selecting the item from GridView1 disables the TextBox1 just fine, but when I click the Update button on the FormView1 then all TextBoxes are enabled, even if I see in the debugger code running through the GridView1_SelectedIndexChanged() function. And after I re-select the gridview item, the correct TextBoxes are disabled again.
Using even this code:
<asp:FormView ID="FormView1" runat="server" DataSourceID="ds1" DefaultMode="Edit" OnItemUpdated="FormView1_ItemUpdated">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("col1") %>' />
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("col2") %>' />
<asp:Button ID="Btn1" runat="server" CommandName="Update" Text="Update" />
</EditItemTemplate>
</asp:FormView>
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (UserAccess() == false) {
TextBox box2 = FormView1.FindControl("TextBox2") as TextBox;
box2.Enabled = false;
}
}
protected void FormView1_ItemUpdated(object sender, EventArgs e)
{
GridView1_SelectedIndexChanged(sender, e);
}
Maybe I should disable my Textboxes via another event?
This doesn't make sense, please explain more about why you are trying to disable the TextBox, have you just left the ItemTemplate off in your question? or is it actually missing? if it's missing why?
The TextBox is in the FormView's EditItemTemplate, so will only be visible when the FormView is in edit mode. After clicking update or cancel the TextBox will no longer be rendered, and the ItemTemplate is rendered instead. So there should be no need to set the TextBox to be disabled.
EDIT
Ok since you edited your question. You need to use the OnDataBound event of the FormView, which occurs at the end of binding, and disable your TextBoxes at that point.
aspx
<asp:FormView ID="FormView1" runat="server" DataSourceID="ds1"
OnDataBound="FormView1_DataBound">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("col1") %>' />
</EditItemTemplate>
</asp:FormView>
aspx.cs
protected void FormView1_DataBound(object sender, EventARgs e)
{
if (UserAccess() == false) {
TextBox box2 = FormView1.FindControl("TextBox2") as TextBox;
box2.Enabled = false;
}
}
Rather then using gridview selected Index change you can use DataBound event on formview, so your logic would fire everytime the formview is rebind.

Calling a function in code behind by using an imagebutton in a gridview [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calling a functon in code behind by using an imagebutton in a gridview
I have an ImageButton within a gridview in .aspx on clicking this imagebutton i have to call a function.
This is how i tried and the function was not being called.
Code inside.aspx page:
<GridView ......>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"VehID","mngVeh.aspx?delid={0}") %>'>
<asp:ImageButton runat="server" ID="DeleteUrlImageButton" ImageUrl="~/images/delete.jpeg" width='24' height='24'
OnClick="DeleteUrlImageButton_Click"
OnClientClick="return confirm('Are you sure you want to delete?');" />
</asp:HyperLink>
</GridView>
code in .aspx.cs page:
public void DeleteUrlImageButton_Click(object sender, EventArgs e)
{
//code to perform the necessary action.
}
I think you're mixing controls up here.
I would use an ImageUrl on the asp:hyperlink itself, and also the OnClick property. You can handle the OnClientClick by using javascript inside the NavigateUrl property.
I don't see a reason for having a nested ImageButton - I doubt this even works.
Or, just remove the asp:hyperlink completely and do a redirect inside the OnClick event.
The problem here is twofold: First, the click event does not bubble up through the GridView, which is why the event is not firing. Second, that there will be an ImageButton for each row of the GridView, and you need to identify which row the event originated from (or more accurately, which underlying datasource record that row corresponds to).
A common way to approach this problem is to use the ImageButton.CommandName and ImageButton.CommandArgument properties, and handle the GridView.RowCommand event.
Markup:
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ID="DeleteUrlImageButton" ImageUrl="~/images/delete.jpeg"
Width='24' Height='24' OnClientClick="return confirm('Are you sure you want to delete?');"
CommandName="MyDelete" CommandArgument='<%# Eval("RecordId") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if ((e.CommandName == "MyDelete") && (e.CommandArgument != null))
{
//Add delete code here using e.CommandArgument to identify the correct record.
// For instance:
MyDataObject obj = new MyDataObject();
obj.RecordId = int.Parse(e.CommandArgument.ToString());
MyDataObject.Delete(obj);
}
}
Edit: I see you provided the name of the ID field in your example, so you would probably want to replace "RecordId" in my example with "VehID".

Categories