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

Discussion in 'ASP .Net Web Controls' started by James Radke, Oct 27, 2003.

  1. James Radke

    James Radke Guest

    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
     
    James Radke, Oct 27, 2003
    #1
    1. Advertising

  2. 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.
     
    Jacob Yang [MSFT], Oct 28, 2003
    #2
    1. Advertising

  3. James Radke

    James Radke Guest

    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]" <> wrote in message
    news:ZxI1U%...
    > 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.
    >
     
    James Radke, Oct 28, 2003
    #3
  4. 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.
     
    Jacob Yang [MSFT], Oct 29, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. john
    Replies:
    4
    Views:
    5,045
    Curt_C [MVP]
    Jan 30, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    935
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,128
    Smokey Grindel
    Dec 2, 2006
  4. Pkenty
    Replies:
    0
    Views:
    420
    Pkenty
    May 28, 2004
  5. Jason C
    Replies:
    4
    Views:
    702
    Morty Abzug
    Jun 26, 2012
Loading...

Share This Page