Could not access controls in codebehind page - c#

I declared Linkbutton control in Gridview but in code behind i could not access that. Below is my aspx page code.
<%# Page="" Language="C#" AutoEventWireup="true" MasterPageFile="~/MainMaster.Master"
CodeBehind="Page.aspx.cs" Inherits="IntakeLibrary.Page" %>
<%# Register="" Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="updatePanel" runat="server">
<contenttemplate>
<asp:GridView ID="grdView" runat="server">
<Columns>
<asp:TemplateField HeaderText="Text">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</contenttemplate>
</asp:UpdatePanel>
</asp:Content>
Below is my codebehind code.
protected void Page_Load(object sender, EventArgs e)
{
LinkButton1.Text = "Test";
}
Below is the error i am getting
The name 'LinkButton1' does not exist in the current context

You don't have a LinkButton on your page - you have a LinkButton in the ItemTemplate in one column of a GridView in the template of your UpdatePanel. You'll need to reach down past all those layers before you'll be able to reference the LinkButton itself.

You are using the wrong method, the correct way is to assign the text in the button control tag itself. This button is inside a gridview so it will be repeated. You cannot reference suppose 10 rendered buttons with one property right? You have to loop through and change their text on row data bound event.

You would probably have to use a DataGrid_RowDataBound event handler and get a handle to the control in the correct template type and then on the item passed into the event handler do a find control on the link button. I'll get a code example up shortly.

Related

Asp.net button is not firing event

I am having an issue with the asp.net button. It is not firing event. I tried setting causes validation to false and removing the java script and validation but it still doesn't work.
<asp:Button ID="Button2" runat="server" Text="Save" onclick="Button2_Click" />
protected void Button2_Click(object sender, EventArgs e)
{
std.AddGuardianInfo(
Convert.ToInt16(DropDownList1.SelectedValue),
TextBox6.Text,
TextBox7.Text,
TextBox8.Text,
TextBox9.Text,
TextBox10.Text);
Response.Redirect("Std_FeeInfo.aspx");
}
Change onclick to OnClick in the markup of the asp:button. so the markup will be like this:
<asp:Button ID="Button2" runat="server" Text="Save" OnClick="Button2_Click" />
And an important advise for you: Use css for styling and arranging elements in your page, giving space using a sequence of will not be a good design
You want to check this steps.
#rabiya are you copied this code in some where else ? Then just remove it and double click on your asp button.
If you are using updatepanel on onclick event, this may happen.So Use 'EnableEventValidation="false"'
Example :
<%# Page Language="C#" AutoEventWireup="true" EnableEventValidation="false" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

How can I get my asp:TreeView control to recognize the TreeNodeCheckChanged method I wrote?

I'm having a really frustrating issue with a TreeView control in a Web Form. I'm using C# on Visual Studio, and, admittedly, this is my first project on the platform, so there are a lot of caveats I'm finding I'm not aware of.
The page I'm working on isn't that complicated. In the body of WebPage.aspx, I have an empty TreeView control declared, along with a Label to display output (for testing) and a Button to cause a postback:
<asp:TreeView ID="SimpleTreeView" runat="server">
</asp:TreeView>
<asp:Label ID="StatusLabel" runat="server">No change yet.</asp:Label>
<asp:Button ID="SaveButton" runat="server" Text="Save Changes" PostBackUrl="~/WebPage.aspx" />
I populate the TreeView programmatically within the Page_Load method of my WebPage class, based on data in our SQL server. That part works like a charm.
Now, I want to define the event handler for the TreeView's TreeNodeCheckChanged event. Firstly, YES, I know that the event does not fire immediately when a checkbox is changed; 99% of everything I can find on the Internet about this is people complaining about that. For my application, waiting until the button is clicked is fine.
I've followed what the MSDN article on the event does, by adding the OnTreeNodeCheckChanged attribute to the TreeView control:
<asp:TreeView ... OnTreeNodeCheckChanged="CheckChangedMethod">
and added a corresponding method to the WebPage class in WebPage.aspx.cs:
protected void CheckChangedMethod( object sender, TreeNodeEventArgs e )
{
StatusLabel.Text = "A checkbox was changed!";
}
When I launch the page, though, I get an error screen that says:
CS1061: 'ASP.webpage_aspx' does not contain a definition for 'CheckChangedMethod' and no extension method 'CheckChangedMethod' accepting a first argument of type 'ASP.webpage_aspx' could be found (are you missing a using directive or an assembly reference?)
I have found that if I move the method to within a tag on WebPage.aspx, it works! Unfortunately, though, I need the method to access classes and methods in other parts of the site, which I can't from within the script tag (...right?). How can I get my event handler recognized by the compiler without removing it from the WebPage class?
Edit:
The total markup looks like this:
<%# Page Title="Web Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireUp="true" CodeBehind="WebPage.aspx.cs" Inherits="Project.WebPage" %>
<%# MasterType VirtualPath="~/Site.Master" %>
<%# Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolKit" %>
<asp:Content ID="Content1" ContentPlaceHolder="HeadContent" runat="server">
<!-- This is where I placed the script tag to test the method -->
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolder="MainContent" runat="server">
<asp:ScriptManager ID="ScriptManager" runat="server"></asp:ScriptManager>
<h2><em>Headline</em></h2>
<br />
<asp:TreeView ID="SimpleTreeView" runat="server" ExpandDepth="0" ShowCheckBoxes="All">
</asp:TreeView>
<asp:Label ID="StatusLabel" runat="server">No change yet.</asp:Label>
<asp:Button ID="SaveButton" runat="server" Text="Save Changes" PostBackUrl="~/WebPage.aspx" />
</asp:Content>
Well... Not sure what happened, but after fixing bugs elsewhere on our site, the code above magically decided to start working. I'm not going to question it.

UpdatePanel cause error: PageRequestManagerServerErrorException

I have a page that works fine with multiple a grid and multiple buttons. The page works fine until I add an asp:UpdatePanel. Then I get the following message pushing any of my buttons:
Sys.WebForms.PageRequestManagerServerErrorException: Input string was not in a correct format.
There is no javascript on the page just straight html.
Here is the page:
<%# Page Title="" Language="C#" MasterPageFile="~/MasterPages/Site.Master" AutoEventWireup="true"
CodeBehind="TestUpdatePanel.aspx.cs" Inherits="ASCWeb.TestUpdatePanel" %>
<asp:Content ID="mHeadContent" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="mBodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="pnlUpdate" runat="server">
<ContentTemplate>
<asp:TextBox ID="txtUser" runat="server" />
<asp:ImageButton ID="btnAdd" runat="server" ImageUrl="~/Images/Add.png" />
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
If I take the TextBox out, it works fine. Nothing is in the code behind.
What would cause this?
Thanks
As per experience, I only encounter that exception when calling Javascript from codebehind, like when using ScriptManager.RegisterClientScriptBlock() to call window.alert(), for instance. But for this issue, I think this resolves it: http://forums.asp.net/t/1823287.aspx/2/10.

Export to Excel control derived from GridView

I have a control that is derived from the GridView class. The BottomPagerRow is always visible. Overriding InitializePager, I added an UpdatePanel and a LinkButton (Click event points to function to convert data) inside the UpdatePanel.
var download = new LinkButton { Text = "Open in Excel", CssClass = "fakeButton", ID = "DownloadToExcel" };
download.Click += DownloadExcelFile;
var up = new UpdatePanel() { ID = "ExcelUpdatePanel" };
var trigger = new PostBackTrigger { ControlID = download.ID };
up.Triggers.Add(trigger);
up.ContentTemplateContainer.Controls.Add(download);
row.Cells[0].Controls.Add(up);
When I click the LinkButton in a page using the control I see the following client side Exception in the web browser,
Uncaught Sys.WebForms.PageRequestManagerParserErrorException:
Sys.WebForms.PageRequestManagerParserErrorException: The message
received from the server could not be parsed.
From my understanding, I think this is because I am using Response.Write in DownloadExcelFile(). However, I thought according to this link that adding the PostBackTrigger would avoid this. I have also replaced the PostBackTrigger generation in the InitializePager event with
ScriptManager.GetCurrent(Page).RegisterPostBackControl(download);
However, the result remains the same, some times it works and other times I see the exception. The exception is visible in a page similar to the following:
<%# Page Title="" Language="C#" MasterPageFile="~/Site.Master"
AutoEventWireup="true" CodeBehind="somePage.aspx.cs" Inherits="somePage" %>
<asp:Content ID="SomeContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
Some other junk...
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server" ID="SomePanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:CustomGridView ID="SomeCustomGridView" runat="server" AutoGenerateColumns="False"
DataSourceID="SomeSqlDataSource" AllowPaging="True" AllowSorting="True">
<Columns>
<asp:BoundField DataField="SomeField" />
</Columns>
</asp:CustomGridView>
<asp:SqlDataSource ID="SomeSqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:SomeConnectionString %>"
SelectCommand="<%$ Resources: SomeResource, Query %>">
<SelectParameters>
<asp:QueryStringParameter Name="SomeId" QueryStringField="SomeId" />
</SelectParameters>
</asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
The master page that this child page uses includes a ToolkitScriptManager. I am using .NET Framework 4. If anyone has input on how to avoid this exception I would appreciate it. Not sure if I'm adding the trigger in the wrong part of the control's lifecycle, or just completely missed the point of the linked article. Thanks.

can't find control in FormView?

I need find this <a> tag resided in a FormView control, I need to remove this a tag depending on the condition but I can't find it using FormView.FindControl method
<asp:UpdatePanel ID="upDiscipline" runat="server">
<ContentTemplate>
<asp:FormView ID="fvMediaIntro" runat="server">
<ItemTemplate>
<div class="clipControls">
<a runat="server" id="iNeedToFindThis" href="#">here</a>
</div>
</ItemTemplate>
</ContentTemplate>
</asp:UpdatePanel>
I tried fvMediaIntro.FindControl() and fvMediaIntro.Row.FindControl(), neither worked.
Any idea please??
FindControl will work only after those controls are created i.e when data is bound to the FormView. So you need to use appropriate event on FormView such ItemCreated or DataBound. For example,
protected void fvMediaIntro_ItemCreated(Object sender, EventArgs e)
{
var control = fvMediaIntro.Row.FindControl("iNeedToFindThis") as HtmlAnchor;
}
Assuming, you are binding in page_load or using mark-up, you can also use prerender event of parent page/control safely to do FindControl.

Categories