Datagrid question! losing a column on page refresh... why?

J

James Radke

Hello,

I am using a datagrid on a asp.net (1.1 version) webform.

In the .aspx page I have a blank ItemTemplate column defined as:

<asp:TemplateColumn HeaderText="Line-DL&lt;br&gt;&lt;br&gt;">
<HeaderStyle HorizontalAlign="Center" Width="40px"
VerticalAlign="Bottom"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" Width="40px"
VerticalAlign="Top"></ItemStyle>
<ItemTemplate></ItemTemplate>
</asp:TemplateColumn>

Then in the code behind, in the OnItemDataBound method I have:

Public Sub DoItemDataBound(ByVal objSource As Object, ByVal objArgs
As System.Web.UI.WebControls.DataGridItemEventArgs)
Dim itemType As System.Web.UI.WebControls.ListItemType =
objArgs.Item.ItemType
If itemType = System.Web.UI.WebControls.ListItemType.Item Or
itemType = System.Web.UI.WebControls.ListItemType.AlternatingItem Then

' Get the current data row in the grid
Dim myRow As DataRowView = CType(objArgs.Item.DataItem,
DataRowView)

If myRow("Status") = 3 Then
Dim x As New LinkButton
x.Text = myRow("LINNUM") & "-" & myRow("DELNUM")
x.CommandName = "Retrieve"
x.CausesValidation = False
x.ID = "lnkLineDL"
objArgs.Item.Cells(0).Controls.Add(x)
Else
objArgs.Item.Cells(0).Text = myRow("LINNUM") & "-" &
myRow("DELNUM")
End If

End If
End Sub

This works great to display the data, as only items with a status code 3
show the linkbutton.

Here is my problem: when I click on the linkbutton (currently no code
behind) the page refreshes and the values in the linkbutton are no longer
displayed for each row of data! Since the databind is not being hit there,
AND the rest of the datagrid retains it's data, I don't understand why the
linkbutton is disappearing from the screen.

Does anyone know what is going on?

Thanks!

Jim
 
J

Jacob Yang [MSFT]

Hi James,

Thank you for using Microsoft MSDN managed newsgroup.

Based on my research and experience, the reason of this issue is that the
linkbutton is created dynamically. We can try a simple testing for this
issue.

1. Create a default C# Web application.

2. Add two buttons on the web form.

3. Add the following code in Button1_Click.

private void Button1_Click(object sender, System.EventArgs e)
{
TextBox myText = new TextBox ( );
myText.Text = "Welcome to ASP.NET";
myText.ID = "myText";
PlaceHolder1.Controls.Add ( myText );
myText.Attributes.Add("style",@"Z-INDEX: 103; LEFT: 16px; POSITION:
absolute; TOP: 128px");

}

4. Do nothing in Button2_Click.

5. Build and run the application.

6. Click Button 1. We can see that the TextBox is created dynamically.

7. Click Button 2. We can see that the TextBox disappears.

To resolve this problem, please make sure that the OnItemDataBound method
is called in Page_Load.

Does it answer your question? If I have misunderstood your concern, please
feel free to let me know.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

James Radke

Jacob,

That brings up a couple of questions:

1.) Is there a control that I can place in the web .aspx page that would
allow me to display both the hyperlink and text, that would not disappear
when the page is refreshed?

2.) How can I call the OnItemDataBound (i.e. hwo do I create the passed
arguments)? Do I have to rebind the datagrid on every page load? If so,
that wouldn't be too efficient....

3.) Why would the refresh "eliminate" a dynamic control? Once it's
created, why wouldn't it stay created on the form?

Jim
 
J

Jacob Yang [MSFT]

Hi James,

I have done more research regarding this issue and reproduced the same
result on my side.

Since the LinkButton was created dynamically in the ItemDataBound event of
the datagrid control, and in addition, the DataBind() on the datagrid was
not invoked in each postback, as you said, the ItemDataBound event will not
be fired in each postback.

In this case, we should do the rebinding on the datagrid control.

Please let me know if it makes sense.

Best regards,

Jacob Yang
Microsoft Online Partner Support
Get Secure! ¨C www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top