Databind fires after ItemUpdated event - c#

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.

Related

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

ASP.Net DataList problem

I have declaratively created a LinqDataSource and DataList and bound them in markup. I have created an ItemTemplate and Edit Template.
I have bound the DataLists oneditcommand and oncancelcommand to methods in the code behind.
<asp:DataList ID="MyDataList" runat="server" DataSourceID="LinqDataSource1" RepeatDirection="Horizontal"
Font-Bold="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
Font-Underline="False" HorizontalAlign="Center" RepeatColumns="4"
oneditcommand="MyDataList_EditCommand"
oncancelcommand="MyDataList_CancelCommand"
>
<ItemTemplate>
<div style="margin: 5px;">
<asp:LinkButton Text="Edit" CommandName="Edit" style="float:right" runat="server" />
// Other markup
</div>
</ItemTemplate>
When I click the LinkButton in the ItemTemplate, it runs the following code:
protected void DataList_EditCommand(object source, DataListCommandEventArgs e)
{
MyDataList.EditItemIndex = e.Item.ItemIndex;
MyDataList.DataBind();
}
This works fine and puts the selected item in the DataList into edit mode.
The Edit Template:
<EditItemTemplate>
<div style="margin: 5px;">
<asp:LinkButton Text="Cancel" style="float:right"
runat="server" CommandName="cancel" CausesValidation="false"/>
//other markup
</div>
</EditItemTemplate>
When I click the cancel button in the edit template, It does not fire the method in the code behind (the breakpoint doesn't get hit).
The Code that should be run when cancelling never get's run, so I can not exit the edit mode back into normal read mode:
protected void MyDataList_CancelCommand(object source, DataListCommandEventArgs e)
{
PhotoDataList.EditItemIndex = -1;
PhotoDataList.DataBind();
}
Can anyone think of a reason for this?
---- UPDATE
It seems that it is just the second firing of an event on the DataList that doesn't work, as I have know bound to the ItemCommand event, and was going to intercept the DataListCommandEventArgs.CommandName property and do something based on that. If you click the Edit link button, the ItemCommand method fires (with no code body at all), but the second time you click the edit link button, the ItemCommand method does not get hit.
The name of your DataList is : "MyDataList"
but Cancel event calls PhotoDataList!
protected void MyDataList_CancelCommand(object source, DataListCommandEventArgs e)
{
PhotoDataList.EditItemIndex = -1;
PhotoDataList.DataBind();
}
I can't duplicate your problem, I ran this demo code and all events and templates worked fine:
How to: Allow Users to Edit Items in DataList Web Server Controls
Do you have any errors in your event logs? Also, I don't see the DataKeyField defined, how are you selecting your data?

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

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.

DetailsView FindControl() returns null after some postbacks

I've been working for a long time with GridViews and DetailsViews, but yesterday I've come across a new scenario, which I quite do not understand.
I have a GridView with ImageButton (CommandName="Insert") which will change the mode of the DetailsView to Insert. Afterwards I'll look for a DropDownList inside that DetailsView and add some items dynamically. Works fine, but one first the first time I press that ImageButton. If I click on "Cancel" in the DetailsView and press the ImageButton again, the .FindControl() Method returns null. What life cycle problem am I facing here?
I've created this sample: (To make it run in your Visual Studio, just bind a DataSource to the DetailsView, otherwise it will not be rendered)
Markup:
<asp:GridView ID="gvCategory" runat="server" OnRowCommand="gvCategory_RowCommand">
<Columns>
</Columns>
<EmptyDataTemplate>
<asp:ImageButton ImageUrl="~/images/add.png" ID="ibAdd" runat="server" CommandName="Insert" />
</EmptyDataTemplate>
</asp:GridView>
<asp:DetailsView ID="dvCategory" runat="server" Width="150px" AutoGenerateRows="false"
AutoGenerateInsertButton="True" DataSourceID="LinqDataSource1">
<Fields>
<asp:TemplateField HeaderText="foo">
<InsertItemTemplate>
<asp:DropDownList ID="ddlCategory" runat="server" Width="150"></asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView><asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="WebApplication1.DataClasses1DataContext"
TableName="Categories"></asp:LinqDataSource>
Codebehind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.gvCategory.DataBind();
}
}
protected void gvCategory_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
this.dvCategory.ChangeMode(DetailsViewMode.Insert);
DropDownList _ddlCat = (DropDownList)this.dvCategory.FindControl("ddlCategory");
if (_ddlCat != null)
{
_ddlCat.Items.Clear();
_ddlCat.Items.Add(new ListItem() { Text = "-- empty --", Value = "-1" });
}
}
}
I have also tried using a ItemTemplate, and not a InsertItemTemplate, but this results in the same. After using the ChangeMode-Method the DetailsView.CurrentMode == InsertMode. The only thing I can think of is, that the markup is already generated for the ItemTemplate and changing the Mode to InsertMode can't affect the rendered markup, or something like this.
Does anybody have a solution to this? =)
I think you are on the right track. It's hard to tell without seeing all of the code, but basically any time you change the rendering mode of a row in a repeater-type control you need to rebind it so that it's re-rendered. The fact that FindControl is returning NULL means only one thing: THE CONTROL IS NOT THERE. Which means it was not rendered. You can verify this by looking at the control hierarchy.
So, in your handler for Cancel are you rebinding?

Categories