Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Datagrid question! losing a column on page refresh... why?

Reply
Thread Tools

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

 
 
James Radke
Guest
Posts: n/a
 
      10-27-2003
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.Alternating Item 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


 
Reply With Quote
 
 
 
 
Jacob Yang [MSFT]
Guest
Posts: n/a
 
      10-28-2003
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.

 
Reply With Quote
 
 
 
 
James Radke
Guest
Posts: n/a
 
      10-28-2003
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

"Jacob Yang [MSFT]" <(E-Mail Removed)> wrote in message
news:ZxI1U%(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
Jacob Yang [MSFT]
Guest
Posts: n/a
 
      10-29-2003
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex losing <br> (different from the earlier topic about losing $1) Jason C Perl Misc 4 06-26-2012 10:29 PM
Losing Drives - Finding Drives - Losing Drives mel@no.spam.com Computer Support 2 09-21-2007 10:16 PM
bind a dropdown in a column in a datagrid based on the dropdown value selected in another column of the datagrid. vishnu ASP .Net 1 03-25-2006 01:24 PM
how do i access a dropdownlists selected value in a datagrid edititemtemplate column from the selectedindexchanged event of another dropdownlist in a datagrid edititemtemplate column Dave M ASP .Net Datagrid Control 0 12-14-2004 11:53 AM
How to bind a db image column (byte array) to a datagrid column? Jim Hammond ASP .Net 1 11-26-2003 12:55 AM



Advertisments