Invalid postback or callback argument (HiddenField and container Visible=false) - c#

I've not found answers that matched my circumstances, so I'm posting an answered question hoping it will help others.
I was getting the error
Invalid postback or callback argument. Event validation is enabled
using in configuration or <%#
Page EnableEventValidation="true" %> in a page. For security
purposes, this feature verifies that arguments to postback or callback
events originate from the server control that originally rendered
them. If the data is valid and expected, use the
ClientScriptManager.RegisterForEventValidation method in order to
register the postback or callback data for validation.
at System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument)
at System.Web.UI.Control.ValidateEvent(String uniqueID, String eventArgument)
at System.Web.UI.WebControls.HiddenField.LoadPostData(String postDataKey, NameValueCollection postCollection)
at System.Web.UI.WebControls.HiddenField.System.Web.UI.IPostBackDataHandler.LoadPostData(String postDataKey, NameValueCollection postCollection)
at System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
I've got a Databound ListView (with a few 100s rows), with buttons on each row. The buttons bring a popup. The popup has dropdownlists and other controls doing asynchronous postbacks. I need to make sure I do asynchronous postbacks to avoid refreshing my big table.
I get the error when I click the button on one row, then change a dropdownlist inside the popup which fires a postback (selected item changed). Boom.
Here's the markup for a reduced sample, without popup and javascript at all! It still exhibits the problem. Click twice on a button in a row to get the error.
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TestPopupAsynchPostback.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptMgr" runat="server" ScriptMode="Debug" EnablePartialRendering="true"
EnableScriptGlobalization="true" EnableScriptLocalization="true" EnablePageMethods="true"/>
<asp:ObjectDataSource ID="ListDataSource" runat="server" SelectMethod="List" TypeName="TestPopupAsynchPostback.Default" />
<asp:Label runat="server" ID="PageLabel"></asp:Label>
<asp:ListView ID="EL" runat="server" DataSourceID="ListDataSource" OnItemDataBound="EntityList_OnItemDataBound">
<LayoutTemplate>
<table border="1">
<tr id="itemPlaceholder" runat="server" enableviewstate="false">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server" id="DefaultRow" enableviewstate="false">
<td>
<asp:Label ID="Lbl" runat="server" EnableViewState="false" />
</td>
<td>
<button runat="server" type="button" id="ReportingButton" enableviewstate="false" onserverclick="ReportingButton_OnClick" causesvalidation="false">click</button>
</td>
</tr>
<%-- Fix part 1: Change SpecialRow visible = true--%>
<tr runat="server" id="SpecialRow" visible="false" enableviewstate="false">
<td>
<asp:Label ID="Lbl2" runat="server" EnableViewState="false" />
<asp:HiddenField runat="server" ID="fn_hid" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
</form>
</body>
</html>
Here's the code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace TestPopupAsynchPostback
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager sm = ScriptManager.GetCurrent(Page);
PageLabel.Text = DateTime.UtcNow.ToString() + " IsPostBack:" + IsPostBack + " IsInAsyncPostBack:" + (sm == null ? "" : sm.IsInAsyncPostBack.ToString());
}
protected override void Render(HtmlTextWriter writer)
{
ScriptManager sm = ScriptManager.GetCurrent(this.Page);
if (sm != null)
{
foreach (ListViewDataItem row in EL.Items)
{
HtmlButton reportingButton = row.FindControl("ReportingButton") as HtmlButton;
if (reportingButton != null)
sm.RegisterAsyncPostBackControl(reportingButton);
}
}
base.Render(writer);
}
public IList<string> List()
{
return (new string[] { "ONE", "TWO"}).ToList();
}
protected void ReportingButton_OnClick(object sender, EventArgs e)
{
//Do something useful here, for now, just a postback event
}
protected void EntityList_OnItemDataBound(object sender, ListViewItemEventArgs e)
{
Label lbl = e.Item.FindControl("Lbl") as Label;
Label lbl2 = e.Item.FindControl("Lbl2") as Label;
lbl.Text = lbl2.Text = e.Item.DataItem.ToString();
HtmlTableRow specialRow = e.Item.FindControl("SpecialRow") as HtmlTableRow;
if (e.Item.DataItemIndex%2 == 0)
{
HiddenField fn_hid = e.Item.FindControl("fn_hid") as HiddenField;
fn_hid.Value = "test1";
specialRow.Visible = true;
}
//Fix part 2: set SpecialRow Visible = false in code behind
//else
// specialRow.Visible = false;
}
}
}

I initially thought my javascript was at fault as I am modifying things with it. However, the process of creating a sample page has helped me find the problem.
It turns out it has nothing to do with the javascript or the popup. It's to do with a HiddenField contained inside a TR HtmlControl (asp.net server side control) with Visible=false IN THE MARKUP. I then use the code behind to set Visible=true when I need to in OnItemDataBound event.
This is what is causing the error for me: It seems that because the container (the TR SpecialRow) is Visible=false, I guess something is not rendered. I then come along in OnItemDataBound, decide that this row must be shown, and set Visible=true and set the value of my HiddenField. Boom.
If I remove the markup for the hidden field and don't set its value, no crash.
So it's not just the visibility of the TR on its own, it's the HiddenField too.
My fix: don't set Visible=false in the markup, and change OnItemDataBound to set Visible=false when required.
In other words: I was defaulting to hide things in markup and use code behind to show them. I changed this around and show things by default in markup and hide them using the code behind.
I've added the fix in the markup and code above.

Related

Programmatically add New button to ASP.Net DetailsView

I have an ASP.Net 4.5.1 web application that has a page with a GridView and a DetailsView. The two controls are linked, so that when a Select button is clicked in the GridView, the DetailsView displays the data for that row. The DetailsView record has an Edit and a Delete button. This all works fine. However, I don't want to add a New button at the bottom of the DetailsView along with the Edit and Delete buttons. I don't see any reason why my user should have to select an existing record before entering a new record into the dataset.
So - I've created an ASP button at the top of the page called btnNew. What this button is supposed to do is open the DetailsView and change its mode to Insert. I have two buttons in the DetailsView FooterTemplate, btnInsert and btnCancel. They are not visible by default, but I make them visible when btnNew is clicked.
Here's the problem: when I click on btnNew the first time, the DetailsView opens in Insert mode and btnInsert and btnCancel display properly. HOWEVER, if I click on btnCancel (the DetailsView and two buttons disappear, all good) and then click on btnNew again, the DetailsView appears, but btnInsert and btnCancel don't. If I click btnNew a second time, they show up. If I move the line in btnNew_Click (see below) that changes the DetailsView mode to Insert to the bottom of the method, then when I click on btnNew the first time, I don't see btnInsert and btnCancel, but when I click on it a second time, they show up. When I walk through the code in Debug mode, btnInsert and btnCancel are found, and their Visible property is set to True, but they aren't displayed.
I've simplified my code down to a page with just a DetailsView (no GridView) with no databinding, with no Master Page, with no Ajax controls, but the problem behavior persists. Below is this simplified code. First my markup:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="SimpleDefault.aspx.cs" Inherits="ElectronicCaseFilingHistory.SimpleDefault" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button runat="server" ID="btnNew" Text="Add New" OnClick="btnNew_Click" BackColor="#F8F8F8" BorderColor="#9BE8E8" ForeColor="#3C3C3C" />
<asp:DetailsView runat="server" ID="dvFilingDetail" AutoGenerateRows="false" >
<Fields>
<asp:TemplateField HeaderText="Attorney" HeaderStyle-Font-Bold="true">
<ItemTemplate>
<asp:Label runat="server" ID="lbAttorneyName" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="tbAttorneyName" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Court" HeaderStyle-Font-Bold="true">
<ItemTemplate>
<asp:Label runat="server" ID="lbCourt" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="tbCourt" />
</EditItemTemplate>
</asp:TemplateField>
</Fields>
<FooterTemplate>
<asp:Button runat="server" ID="btnInsert" Visible="false" Text="Insert" />
<asp:Button runat="server" ID="btnCancel" Visible="false" OnClick="btnCancel_Click" Text="Cancel" />
</FooterTemplate>
</asp:DetailsView>
</div></form></body></html>
Now the code behind:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ElectronicCaseFilingHistory
{
public partial class SimpleDefault : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnNew_Click(object sender, EventArgs e)
{
dvFilingDetail.ChangeMode(DetailsViewMode.Insert);
Button insert = (Button)dvFilingDetail.FindControl("btnInsert");
Button cancel = (Button)dvFilingDetail.FindControl("btnCancel");
if (insert != null)
insert.Visible = true;
if (cancel != null)
cancel.Visible = true;
}
protected void btnCancel_Click(object sender, EventArgs e)
{
dvFilingDetail.ChangeMode(DetailsViewMode.ReadOnly);
dvFilingDetail.DataBind();
}
}
}
I've tried adding an InsertItemTemplate in addition to an EditItemTemplate, but that has no effect. I also added CommandName=New to btnNew and added an ItemCommand event to the DetailsView to try to do it that way, but the ItemCommand method was never hit.
What am I doing wrong?
After changing DetailsViewMode to Insert you have to call DataBind(); as well.
protected void btnNew_Click(object sender, EventArgs e)
{
dvFilingDetail.ChangeMode(DetailsViewMode.Insert);
dvFilingDetail.DataBind();
Button insert = (Button)dvFilingDetail.FindControl("btnInsert");
Button cancel = (Button)dvFilingDetail.FindControl("btnCancel");
if (insert != null)
{
insert.Visible = true;
}
if (cancel != null)
{
cancel.Visible = true;
}
}

javascript textbox_changed value

here is my ASPX code
<%# Page Title="About Us" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="About.aspx.cs" Inherits="About" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script language="javascript" type="text/javascript">
function sum(t1)
{
var txt1 = document.getElementById('MainContent_TextBox2').value;
var result = parseInt(txt1) / 2;
var result1 = parseInt(result);
if (!isNaN(result))
{
document.getElementById(t1).value = result1;
}
}
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:TextBox ID="TextBox2" runat="server" onkeyup="sum('MainContent_TextBox3')"</asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server" ReadOnly="True"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click"/>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
and my c# code is that
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class About : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
TextBox4.Text = TextBox3.Text;
}
}
the problem is that when i click on Button1 no error and TextBox4 is empty and i want that when i click on Button1 textbox3 value is transfered in textbox4.
It's because of the ReadOnly="True" that you have set on TextBox3.
When a control is set to ReadOnly in ASP.NET, the value that was placed into the control when the page was original rendered will not be updated.
Even if you change the value in the control on the browser (as you are doing), the value that is sent back to the server will not effect the value in the TextBox control - it will keep it's old value thanks to the ViewState.
The answer is to store the new value in a <asp:HiddenField>, which you should update at the same time as TextBox3.
Then on the server use the value in the hidden field, as that will contain the value you want. (Also, remember that you will need to update the value in the textbox from the hidden control as part of the postback).

FindControl can't find a control

I have a problem with FindControl function. The problem is as follow:
aspx:
<asp:Content ID="MainContent" ContentPlaceHolderID="MainContent" runat="server">
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>
<table class="inputTable">
<tr><td>
<asp:CheckBox ID="Extern" AutoPostBack="True" OnCheckedChanged="OnCheckedChangedMethod" runat="server" />
</td><td>Externes Unternehmen</td></tr>
<tr>
<td>
<asp:TextBox ID="Firmierung" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="Firmierung" Display="Dynamic"
ErrorMessage="RequiredFieldValidator"
Text="Bitte geben Sie die Firmierung ein."></asp:RequiredFieldValidator>
</td>
</tr>
</table>
aspx.cs:
protected void OnCheckedChangedMethod(object sender, EventArgs e)
{
if (Extern.Checked)
{
Control ctr = FindControl("RequiredFieldValidator1");
if (ctr != null)
{
ctr.Visible = false;
}
}
else
{
}
}
But FindControl didn't work, it couldn't find that control. Was I wrong at any point?
Thanks in advance.
ASP.NET creates a field for you, as it is located inside a Content: this.RequiredFieldValidator1 in your page.
The FindControl way would be like this (find it in the master page's content panel):
Control ctr = Master.FindControl("MainContent")
.FindControl("RequiredFieldValidator1");
Based on your limited source, you should be able to simplify your code behind method to:
protected void OnCheckedChangedMethod(object sender, EventArgs e)
{
this.RequiredFieldValidator1.Visible = this.Extern.Checked;
}
There should be no need for the use of FindControl().
When you type "this.", if you don't see RequiredFieldValidator1 appear in your intellisense, and assuming you are using ASP.NET 2.0 or greater, check your VS.NET warnings to see if your .aspx has a warning message with an associated "Generation of designer file failed". If so, you must correct the warning.

Refreshing UpdatePanel after click event of a Button inside a modalpopup inside a UserControl

I have a page with a listbox and a user control placed inside an update panel like this:
<%# Register TagPrefix="uc1" TagName="CMessage" Src="~/Controls/CMessage.ascx" %>
<ajaxToolkit:ToolkitScriptManager runat="server" ID="ToolkitScriptManager1" EnableScriptGlobalization="true" EnableScriptLocalization="true" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ListBox ID="lbox" runat="server"></asp:ListBox>
<asp:Button ID="btnDelete" OnClick="btnDelete_Click" runat="server" Text="Delete selected item" Enabled="True"></asp:Button>
<uc1:CMessage ID="CMessage1" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
The page codebehind is like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lbox.Items.Add("test1");
lbox.Items.Add("test2");
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
CMessage.MessageConfirm("Delete item?", "Yes", "No", DeleteItem);
}
protected void DeleteItem()
{
lbox.Items.Remove(lbox.SelectedItem);
CMessage.Message("Item deleted succesfully!");
}
The user control is like this:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="CMessage.ascx.cs" Inherits="Controles.CMessage" %>
<table id="tableMessage" runat="server" style="display: none" class="modalPopup">
<tr>
<td>
<asp:Label ID="lb5" runat="server" Text="Message"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label ID="lbMessage" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnOk" runat="server" Text="Ok" OnClick="btnOk_Click" />
<asp:Button ID="btnCancel" runat="server" Text="Cancel" OnClick="btnCancel_Click" />
</td>
</tr>
</table>
<asp:Button ID="btnOKError" runat="server" Text="OK" style="display: none" />
<ajaxToolkit:ModalPopupExtender ID="ModalPopupMessage" runat="server" TargetControlID="btnOKError" PopupControlID="tableMessage" OkControlID="btnOKError" CancelControlID="btnOKError"></ajaxToolkit:ModalPopupExtender>
The usercontrol codebehind is like this:
public partial class CMensaje : UserControl
{
public delegate void FunctionButtonPressed();
private FunctionButtonPressed FunctionButtonPressedOk
{
get { return (FunctionButtonPressed)Session["FunctionButtonPressedOk"]; }
set { Session["FunctionButtonPressedOk"]= value; }
}
protected void btnOk_Click(object sender, EventArgs e)
{
ModalPopupMenssage.Hide();
if (FunctionButtonPressedOk!= null)
{
FunctionButtonPressedOk();
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
ModalPopupMessage.Hide();
}
public void Mensaje(string message)
{
lbMessage.Text = message;
FunctionButtonPressedOk= null;
btnCancel.Visible = false;
ModalPopupMessage.Show();
}
public void MessageConfirm(string message, FunctionButtonPressed FunButtonOkx)
{
lbMessage.Text = message;
FunctionButtonPressedOk= FunBotonAceptarx;
btnCancel.Visible = true;
ModalPopupMensaje.Show();
}
}
Everything works, the popup is shown, the function to call from the page is passed to the usercontrol to trigger it if the user presses ok and triggers correctly etc. But in that last function DeleteItem() the changes done to the page (in this example the item removed from the listbox and the notification message launched) doesnt work.
Its like the updatepanel doesnt refresh. I cant call manually UpdatePanel1.Update() because i have the updatemode as always. In theory in this part of the pagecycle it should refresh the pages with the changes...
I tried adding in the user control pageload the OK button as a PostBackTrigger of the UpdatePanel with no avail and as an AsyncPostBackTrigger.
Keep in mind that this is an slimmed down version of the page and the user control so people can focus on the problem at hand so if you need any more details ask me...
The code flow is like this:
User clicks in the delete item button and triggers the
btnDelete_Click() in the page (first postback)
btnDelete_Click() calls the usercontrol CMessage.MessageConfirm passing DeleteItem as
argument
At the usercontrol MessageConfirm() shows the modalpopup and saves
the argument function DeleteItem() in order to call it later(end of
first postback)
After displaying the modalpopup the user clicks its Ok button and
triggers the btnOk_Click() in the usercontrol (second postback)
At the usercontrol btnOk_Click() calls the DeleteItem() function
that was saved previously
At the page DeleteItem() removes the item from the ListBox (lets say
i dont call the second message to simplify, this would be the end of
the second postback, and the update panel hasnt refreshed :S)
I think i have isolated the problem, moved all the controls and functions from the usercontrol to the page, the problem persisted, but if i called the DeleteItem() function directly instead of his delegate it worked (keep in mind that in both cases the breakpoint at DeleteItem() triggered so the function code was executing correctly):
protected void btnOk_Click(object sender, EventArgs e)
{
ModalPopupMenssage.Hide();
/*if (FunctionButtonPressedOk!= null)
{
FunctionButtonPressedOk();
}*/
DeleteItem(); //page updates correctly! why?
}
Is there some incompatibility with delegates?
In the end i solved it changing the following lines:
if (FunctionButtonPressedOk!= null)
{
//FunctionButtonPressedOk();
Page.GetType().InvokeMember(FunctionButtonPressedOk.Method.Name, BindingFlags.InvokeMethod, null, Page, new []{sender, e});
}
Why it works this way?Whats the difference internally?

ASP/C#, adding text to textbox problem

For some reason, I cannot get text into any textbox or label!
I'm using Master pages and the code is going in the code behind view. I have created the textbox:
<asp:Textbox ID="whatever" runat="Server">
When I want to add some text I simply add the code in the code behind view like:
whatever.Text = "myText";
I get an error that says:
"System.NullReferenceException:Object reference not set to an instance of an object"
hightlighting this line in red: whatever.Text = "myText";
I guess its because it saying it not there but how can it let me reference the textbox?
Apologies if the answer is on the site, I have searched but found nothing. :)
This is my code in Basket.asp - I've changed the textbox to a label, it's called bskItems
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder3" runat="server">
<asp:Label ID="bskItems" runat="server"></asp:Label>
<div id="cart">
<asp:Button ID="btnCheckout" CssClass="BasketBtnAdd" runat="server" CommandName="checkout" Text="Checkout" />
</div>
</asp:Content>
This is my masterpage, where I'm using a loginView. ContentPlaceHolder3 is where the textbox should be. I only want it to display a count of items.
<asp:LoginView ID="loginView" runat="server">
<LoggedInTemplate>
<asp:LoginName ID="loginName" runat="server" FormatString="Hi, {0}!"/>
(<asp:LoginStatus ID="loginStatus" runat="server" />)
<%
if (HttpContext.Current.User.IsInRole("Admin"))
{
%>
<asp:SiteMapDataSource ID="admin" SiteMapProvider="admin" runat="server" ShowStartingNode="false" />
<asp:Menu ID="Menu" runat="server" DataSourceID="admin">
<StaticItemTemplate>
<%# Eval("Text") %>
</StaticItemTemplate>
</asp:Menu>
<%
}
if (HttpContext.Current.User.IsInRole("Users"))
{
%>
<asp:SiteMapDataSource ID="user" runat="server" SiteMapProvider="user" ShowStartingNode="false" />
<asp:Menu ID="Menu1" runat="server" DataSourceID="user">
<StaticItemTemplate>
<%# Eval("Text") %>
</StaticItemTemplate>
</asp:Menu>
<%
}
%>
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server"></asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server"></asp:ContentPlaceHolder>
</LoggedInTemplate>
<AnonymousTemplate>
<asp:LoginStatus ID="loginStatus" runat="server" />
<asp:SiteMapDataSource ID="anon" runat="server" SiteMapProvider="anon" ShowStartingNode="false" />
<asp:Menu ID="Menu2" runat="server" DataSourceID="anon">
<StaticItemTemplate>
<%# Eval("Text") %>
</StaticItemTemplate>
</asp:Menu>
</AnonymousTemplate>
</asp:LoginView>
In addition to the other answers, if you're setting the value in Page.OnLoad, remember that the Master page controls haven't been created yet.
Here's a complete layout of the order in which things happen: Complete Lifecycle of an ASP Page
What I usualy do is to make the control visible as a property of my MasterPage.
On the master page (AMasterPage.master):
public TextBox MyTextBox { get { return this.theTextBoxControl; } }
So then, on a child using this masterPage (APage.aspx) :
((AMasterPage)this.Master).MyTextBox.Text = "myText";
When accessing Master Page members from Code-Behind in a Content Place Holder file, I believe you need to do:
this.Master.whatever.Text = "new Text";
Check this link on ASP.NET Master Pages, from MSDN.
You need to do get a reference to the textbox on the master page, then set the text
TextBox tb = Master.Page.FindControl("whatever") as TextBox;
if(tb != null)
{
tb.Text = "myText";
}
Set the ClientIDMode on the textbox to "Static". When the page is rendered it assigns the TextBox's ID to something random. By changing the ClientIDMode to "Static", you should be able to reference the ID because the ID will stay the same and not change.
Or try adding an OnDataBinding event handler and casting the "sender" as a (TextBox). For example:
protected void TextBox_OnDataBinding(object sender, EventArgs e)
{
var txt = (TextBox)sender;
txt.Text = "Something";
}
This should talk to the control directly.

Categories

Resources