Why is a control not found in an updatepanel - c#

I have a button in a GridView inside an UpdatePanel.
I get the following error when I run the page:
A control with ID 'btnShowDepend' could not be found for the trigger in UpdatePanel 'TasksUpdatePanel'.
How do I resolve the issue.

Button btnShowDepend=(Button)TasksUpdatePanel.FindControl("btnShowDepend");
Please use this
Or Maybe you have to put btnShowDepend out of updatepanel

Add one more updatepanel within ContentTemplate
<asp:UpdatePanel ID="updButton" runat="server" UpdateMode="Conditional">
<asp:ImageButton ID="btnShowDepend" runat="server" OnCommand="btnShowDepend_Command" />
</ContentTemplate>

you need to register that Image button as an AsyncPostbackTrigger.
Try this one in RowDataBound
protected void yourTasksGV_RowDataBound(object sender, GridViewRowEventArgs
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton ib = e.Row.FindControl("btnShowDepend") as ImageButton;
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(ib);
}
}

You better add your async trigger in the RowDataBound event:
void yourTasksGV_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton ib = e.Row.FindControl("btnShowDepend") as ImageButton;
if (ib != null)
{
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = ib.UniqueID;
trigger.EventName = "Click";
TasksUpdatePanel.Triggers.Add(trigger);
}
}
}

Related

Cant find a combobox control in edittemplate in Gridview

I am trying to find this control in the Gridview's edititemtemplate section.
<EditItemTemplate>
<ajaxToolkit:ComboBox ID="GridviewCategoryComboBox1" AppendDataBoundItems="true" runat="server" AutoCompleteMode="Suggest" DataSourceID="GridViewCategorySqlDataSource1" DataTextField="Name" DataValueField="Id" MaxLength="0" Style="display: inline;">
<asp:ListItem>Select Category</asp:ListItem>
</ajaxToolkit:ComboBox>
Here is the event handler where I try to fetch the control that is in the edititem template.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs
e)
{
GridView1.EditIndex = e.NewEditIndex;
int id = (int)GridView1.DataKeys[e.NewEditIndex].Value;
ComboBox ddl = GridView1.Rows[e.NewEditIndex].Cells[1].FindControl("GridviewCategoryComboBox1") as ComboBox;
}
It returns null, no matter who I try to find it.
I also tried other variations such as this:
ComboBox ddl = GridView1.Rows[e.NewEditIndex].FindControl("GridviewCategoryComboBox1") as ComboBox;
You can use the RowDataBound event for this:
protected void GridView1_RowDataBound(object sender, GridViewEditEventArgs e) {
if (e.Row.RowType == DataControlRowType.DataRow) {
if ((e.Row.RowState & DataControlRowState.Edit) > 0) {
ComboBox ddl = (ComboBox)e.Row.FindControl("GridviewCategoryComboBox1");
}
}
}
Because it is likely that you have other code in the RowDataBound event, then this allows you to centralize all your code in that event and avoid duplicate code.

Setting Button in listview Visibility to False if query result is "0"

I am building a web application in ASP.net and I have a little problem.
I have a LISTVIEW to display data from a data source, and in that listview I have included a BUTTON in every row to be visible if the result of the query in the Page_load is 0.
The Query works, but I don't know how to select the button in the query.
I have tried
ListView1.FindControl("hiddenButton").visible = false;
this is the buttons code
<asp:Button ID="hiddenButton" runat="server" CommandArgument ='<%# Eval("ProfileId") %>' Text="Add Friend" CssClass="btn btn-info pull-right" OnClick="addFriend_Click" Width="105px" allign="right"/>
But its not working.
You can do this in ItemDataBound event:-
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType==ListViewItemType.DataItem)
{
if (YourCondition)
{
Button hdn = (Button)e.Item.FindControl("hiddenButton");
hdn.Visible = false;
}
}
}
You need to associate this event handler in your mark-up(if not already done):-
<asp:ListView ID="ListView1" OnItemDataBound="ListView1_ItemDataBound">
</asp:ListView>
You can use ItemDataBound event To set Buttons visible to True/False
protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ListViewDataItem dataItem = (ListViewDataItem)e.Item;
if (e.Item.ItemType == ListViewItemType.DataItem)
{
Button hiddenButton=(Button) dataItem.FindControl("hiddenButton");
hiddenButton.Visible = false;
}
}

codebehind is not setting CssClass on a link button in a repeater

New to webforms/c# I get a reference to the object in the onclick handler.
The reference to the object exists in the Repeater_ItemDataBound method.
After the curObj.CssClass = "XXXXX" has run the class object curObj is updated.
The page renders without the CSS class applied to the object.
I assume this is due to the LinkButton CSS does not apply to the Anchor tag that gets rendered in the end.
So how do I apply the CSS class to the actual rendered Anchor from the code behind?
// my aspx
<asp:Repeater ID="Repeater1" runat="server" onItemDataBound="Repeater_ItemDataBound">
<ItemTemplate>
<asp:LinkButton ID="my_btn" runat="server" OnCommand="cmdSelect_click" CommandArgument='<%# Eval("value") %>'><%# Eval("value") %></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
// my code behind
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (((MyObject)e.Item.DataItem).value == CurrentValue )
{
curObj.CssClass = "someCssClassHere";
}
}
protected LinkButton curObj;
protected void cmdSelect_click(object sender, CommandEventArgs e)
{
curObj = (LinkButton)sender;
CurrentValue = int.Parse(e.CommandArgument.ToString())-1;
}
I dont understand when/where you want to set cssclass..
If you want to set it in ItemDataBound:
protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
LinkButton my_btn = (LinkButton)e.Item.FindControl("my_btn");
if (my_btn != null) my_btn.CssClass = "someCssClassHere";
}
or if you want to set after Click:
protected void cmdSelect_click(object sender, CommandEventArgs e)
{
LinkButton my_btn = (LinkButton)sender;
my_btn.CssClass = "someCssClassHere";
}
That's not quite how templated controls such as the Repeater work.
Firstly, you should do a FindControl inside ItemDataBound to find your LinkButton, and apply the CSS to the item that is found.
Secondly, you don't wire up events for controls inside a Repeater that way; instead you handle the ItemCommand event of the Repeater.
Can you post the code you're using to bind the repeater ? It would be useful to know what your DataSource is, then I can post something that works.
This post might help as well - Linkbutton inside Repeater for paging ASP.Net

How to get Text property of TextBox after ItemCommand event

I have a TextBox control inside a panel and this panel is inside DataList ItemTemplate.
After firing the ItemCommand event, everything works fine except that the TextBox.Text property is always an empty string "" although there is some text in it.
I tried several ways but without success. I would really appreciate if someone can assist me with this. Simplified code is shown below.
Thank you!
ASPX page:
<asp:DataList ID="dlDataList" runat="server" onitemcommand="dlDataList_ItemCommand">
<ItemTemplate>
<asp:Panel ID="pnlReply" runat="server" Visible="False">
<asp:TextBox ID="txtTextBox" runat="server"></asp:TextBox><br />
<asp:LinkButton ID="lnkbtnSend" CommandName="Send" runat="server">Send</asp:LinkButton>
</asp:Panel><br />
<asp:LinkButton ID="OpenPanel" CommandName="OpenPanel" runat="server">Open panel</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</asp:Content>
ASPX.CS Page code behind
protected void Page_Load(object sender, EventArgs e)
{
FillDataList();
}
private void FillDataList()
{
List<string> list = new List<string>();
list.Add("First");
list.Add("Second");
list.Add("Third");
dlDataList.DataSource = list;
dlDataList.DataBind();
}
protected void dlDataList_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "OpenPanel")
{
Panel pnlReply = (Panel)e.Item.FindControl("pnlReply");
pnlReply.Visible = true;
}
if (e.CommandName == "Send")
{
TextBox txtTextBox = (TextBox)e.Item.FindControl("txtTextBox");
//I tried this way also..
//TextBox txtTextBox = (TextBox)e.item.FindControl("pnlReady").FindControl("txtTextBox");
Label1.Text = txtTextBox.Text;
}
}
Please use IsPostBack in page load event. Without it your FillDataList(); is executing on every postback and resetting your DataList.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
FillDataList();
}
}

event attched to link button inside gridview is not working?

I have a grid in asp.net, inside the asp.net i am binding data as linkbutton when clicking on link button I need to call a method in code behind. the attached event is not woking in my code. how i can solve this?
my code is similar like this,
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton link = new LinkButton();
link.Text = e.Row.Cells[0].Text;
link.CommandArgument = "Hello";
link.Click += new EventHandler(this.onLinkClick);
e.Row.Cells[0].Controls.Add(link);
}
}
protected void onLinkClick(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
string value = btn.CommandArgument;
TextBox1.Text=value;
}
You have to call the function that binds the source to the GridView everytime in the Page Load
ex.
protected void Page_Load(object sender, EventArgs e)
{
PopulateGridView();
}
Because there is not logic for adding or not the link button(I guess you have to add it for each record) why don't you add it at design time?
<asp:GridView ID="GridView1" runat="server">
....
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>
</ItemTemplate>
......
</asp:GridView>
Make sure that AutoEventWireup="true" on the page
Handle RowCommand event of GridView to handle "events" of buttons and you are adding LinkButton dynamically then Data Binding must be performed either at Page_Init or Page_Load.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton link = new LinkButton();
link.Text = e.Row.Cells[0].Text;
link.CommandArgument = "Hello";
e.Row.Cells[0].Controls.Add(link);
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string value = e.CommandArgument.ToString();
TextBox1.Text=value;
}
You need to hook up the event handler for your dynamic button in the GridView's RowCreated event or it won't fire. Then use "FindControl" in the RowDataBound event handler. Personally, I don't like this model at all but sometimes it's unavoidable.
You should use the GridView's <asp:ButtonField> with the grid's RowCommand event. This way, you're not the one creating the dynamic control and wiring up the events.
Here's an article on how to use it.

Categories

Resources