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.dtd ">
<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"
autogeneratecolumns="False"
onitemcreated="DataGrid1_ItemCreated"
onprerender="DataGrid1_PreRender">
<columns>
<asp:templatecolumn>
<itemstyle></itemstyle>
<itemtemplate>
<asp:linkbutton runat="server" id="Linkbutton1"
oncommand="LinkButton_Command" commandname="OnNameClicked"
commandargument='<%# DataBinder.Eval(Container.DataItem,"column1")%>'>
<%# DataBinder.Eval(Container.DataItem, "column1") %>
</asp:linkbutton>
</itemtemplate>
<headertemplate>
<table>
<tr>
<td>
<asp:textbox runat="server" id="tbName"/>
</td>
</tr>
</table>
</headertemplate>
</asp:templatecolumn>
</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.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Literal Literal1;
System.Web.UI.WebControls.TextBox tb;
string _commandArg, _commandName;
bool _linkButtonClicked;
protected void Page_Load(object s, EventArgs e)
{
if(!IsPostBack)
{
System.Data.DataTable dt = new System.Data.DataTable();
System.Data.DataRow 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.DataSource = dt.DefaultView;
DataGrid1.DataBind();
}
}
protected void LinkButton_Command(object s,
System.Web.UI.WebControls.CommandEventArgs e)
{
// Store the info in the private fields
// for the later processing:
_commandArg = (string)e.CommandArgument;
_commandName = e.CommandName;
_linkButtonClicked = true;
}
protected void DataGrid1_ItemCreated(object s,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType ==
System.Web.UI.WebControls.ListItemType.Header)
{
System.Web.UI.WebControls.TextBox tbName =
e.Item.FindControl("tbName") as
System.Web.UI.WebControls.TextBox;
if(tbName != null)
{
if(!IsPostBack)
tbName.Text = "Gotcha";
// Let's store the reference to this ghost:
tb = tbName;
}
}
}
protected void DataGrid1_PreRender(object s, EventArgs e)
{
if(_linkButtonClicked)
{
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.193934$oR5.2582@pd7tw3no...
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