Hi, Yan,
The only place I could get reference to the textbox in the header was the
OnItemCreated event. The problem is that it happens after the call to the
event handler of the LinkButtons Click event. Apart from this, the viewstate
gets loaded after the OnItemCreated event. So, the only way is to append
info in the both event handlers and then do the processing in a third event
handler - after the viewstate has been loaded. I chose the OnPreRender event
of the datagrid for this.
Well, this seems to be a dirty solution... But it is working
listing <text.aspx>:
<%@ Page language="C#" inherits="test_ aspx" %>
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
"http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dt d ">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Test</title>
</head>
<body>
<form id="form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" runat="server"
autogeneratecol umns="False"
onitemcreated=" DataGrid1_ItemC reated"
onprerender="Da taGrid1_PreRend er">
<columns>
<asp:templateco lumn>
<itemstyle></itemstyle>
<itemtemplate >
<asp:linkbutt on runat="server" id="Linkbutton1 "
oncommand="Link Button_Command" commandname="On NameClicked"
commandargument ='<%# DataBinder.Eval (Container.Data Item,"column1") %>'>
<%# DataBinder.Eval (Container.Data Item, "column1") %>
</asp:linkbutton>
</itemtemplate>
<headertemplate >
<table>
<tr>
<td>
<asp:textbox runat="server" id="tbName"/>
</td>
</tr>
</table>
</headertemplate>
</asp:templatecol umn>
</columns>
</asp:datagrid>
the posted value is:
<asp:literal id="Literal1" runat="server"> n/a</asp:literal>
</form>
</body>
</html>
listing <test_aspx.cs >:
using System;
public class test_aspx : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Data Grid DataGrid1;
protected System.Web.UI.W ebControls.Lite ral Literal1;
System.Web.UI.W ebControls.Text Box tb;
string _commandArg, _commandName;
bool _linkButtonClic ked;
protected void Page_Load(objec t s, EventArgs e)
{
if(!IsPostBack)
{
System.Data.Dat aTable dt = new System.Data.Dat aTable();
System.Data.Dat aRow dr;
dt.Columns.Add( "column1", typeof(string)) ;
dr = dt.NewRow();
dr[0] = "One";
dt.Rows.Add(dr) ;
dr = dt.NewRow();
dr[0] = "Two";
dt.Rows.Add(dr) ;
dr = dt.NewRow();
dr[0] = "Three";
dt.Rows.Add(dr) ;
dr = dt.NewRow();
dr[0] = "Four";
dt.Rows.Add(dr) ;
dr = dt.NewRow();
dr[0] = "Five";
dt.Rows.Add(dr) ;
DataGrid1.DataS ource = dt.DefaultView;
DataGrid1.DataB ind();
}
}
protected void LinkButton_Comm and(object s,
System.Web.UI.W ebControls.Comm andEventArgs e)
{
// Store the info in the private fields
// for the later processing:
_commandArg = (string)e.Comma ndArgument;
_commandName = e.CommandName;
_linkButtonClic ked = true;
}
protected void DataGrid1_ItemC reated(object s,
System.Web.UI.W ebControls.Data GridItemEventAr gs e)
{
if(e.Item.ItemT ype ==
System.Web.UI.W ebControls.List ItemType.Header )
{
System.Web.UI.W ebControls.Text Box tbName =
e.Item.FindCont rol("tbName") as
System.Web.UI.W ebControls.Text Box;
if(tbName != null)
{
if(!IsPostBack)
tbName.Text = "Gotcha";
// Let's store the reference to this ghost:
tb = tbName;
}
}
}
protected void DataGrid1_PreRe nder(object s, EventArgs e)
{
if(_linkButtonC licked)
{
DoProcessing();
}
}
void DoProcessing()
{
// Do here the actual processing here
Literal1.Text = tb.Text;
}
}
Hope this helps
Martin
"Yan Wang" <yw*******@shaw .ca> wrote in message
news:eimhc.1939 34$oR5.2582@pd7 tw3no...
Hi! Martin:
Thanks a lot for your reply. From code here, I think you put "Gotcha" in
code. What I want
is that I want to get the content of that textbox. Say, I type in
something to that textbox from
browser. Then I click "One" (a link button in datagrid item). The page is
posted back to server.
Now, I want to get the content of TextBox in HeaderTemplate at this time.
But I find, I
can not get it. It always return me empty string.
Do you know how to solve it?
Thanks a lot!
Yan