strange problem with EditItemTemplate

Discussion in 'ASP .Net Web Controls' started by Ben, Aug 17, 2006.

  1. Ben

    Ben Guest

    Hi,

    It's about a table with two fields: 'team' and 'color'. Each team has a
    color.
    What I want is to update the field 'color' in the gridview only with the
    selected value of the dropdownlist, which is bound to a table ('colortable')
    with one field ('allcolor') containing all available colors.

    My problem is this:
    When clicking on 'Edit' button in the gridview, the value shown into the
    <EditItemTemplate> in the gridview is always the first element of the
    dropdownlist ('pickup'). When clicking on 'Update' button, the field 'color'
    becomes empty.

    In the <body>, i put <%=dd%> for testing the selected value of the
    dropdownlist, and the value is right on each click in the dropdownlist. So
    why does Text='<%#dd%>'in the <EditItemTemplate> only show the first value
    and why does the update not work ?

    Thanks for any help
    Ben

    code in aspx:
    -------------
    <body>
    this is used as test and shows the selected value of the dropdownlist:<%=dd
    %>
    .....
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="..."
    SelectCommand="SELECT * FROM [mytable]"
    OldValuesParameterFormatString="original_{0}"
    ProviderName="System.Data.OleDb"
    UpdateCommand = "UPDATE [mytable] SET [color] = ? where [team] = ?" >
    <UpdateParameters>
    <asp:parameter Name="team" Type="String" />
    <asp:parameter Name="color" Type="String" />

    <asp:parameter Name="original_team" Type="String" />
    <asp:parameter Name="original_color" Type="String" />
    </UpdateParameters>
    </asp:SqlDataSource>

    <asp:GridView ID="GridView1" runat="server" DataKeyNames="field1"
    DataSourceID="SqlDataSource1" >
    <Columns>
    <asp:CommandField ShowEditButton="True"/>
    <asp:BoundField DataField="team" />
    <asp:TemplateField HeaderText="color">
    <ItemTemplate>
    <asp:Label ID="kl" Runat="server" Text='<%# Bind("color")
    %>'></asp:Label>
    </ItemTemplate>

    <EditItemTemplate>
    <asp:TextBox ID="kl" ReadOnly="True" Runat="server" Text='<%#dd%>'>
    </asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>

    <asp:DropDownList ID="drd"
    runat="server" AutoPostBack="True">
    </asp:DropDownList>
    .....

    code-behind:
    -----------
    friend dd as string
    ....
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    Handles Me.Load
    Dim ...
    Dim ...
    sConnectionString = "Provider = ...
    sql = "SELECT [allcolor] FROM [colortable];"
    d = New OleDbDataAdapter(sql, sConnectionString)
    ds = New DataSet()
    x = d.Fill(ds)
    If Page.IsPostBack Then
    dd = drd.SelectedValue
    drd.Items.Clear()
    End If
    z = New ListItem("pick up a color", "pickup")
    drd.Items.Add(z)
    For i = 0 To x - 1
    kl = ds.Tables(0).Rows(i).Item(0)
    z = New ListItem(kl, kl)
    drd.Items.Add(z)
    Next
    End Sub
    End Class
     
    Ben, Aug 17, 2006
    #1
    1. Advertising

  2. Ben

    Guest

    Hi Ben, I am having the same issue. Did you ever figure this one out?

    Ben wrote:

    > Hi,
    >
    > It's about a table with two fields: 'team' and 'color'. Each team has a
    > color.
    > What I want is to update the field 'color' in the gridview only with the
    > selected value of the dropdownlist, which is bound to a table ('colortable')
    > with one field ('allcolor') containing all available colors.
    >
    > My problem is this:
    > When clicking on 'Edit' button in the gridview, the value shown into the
    > <EditItemTemplate> in the gridview is always the first element of the
    > dropdownlist ('pickup'). When clicking on 'Update' button, the field 'color'
    > becomes empty.
    >
    > In the <body>, i put <%=dd%> for testing the selected value of the
    > dropdownlist, and the value is right on each click in the dropdownlist. So
    > why does Text='<%#dd%>'in the <EditItemTemplate> only show the first value
    > and why does the update not work ?
    >
    > Thanks for any help
    > Ben
    >
    > code in aspx:
    > -------------
    > <body>
    > this is used as test and shows the selected value of the dropdownlist:<%=dd
    > %>
    > ....
    > <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="..."
    > SelectCommand="SELECT * FROM [mytable]"
    > OldValuesParameterFormatString="original_{0}"
    > ProviderName="System.Data.OleDb"
    > UpdateCommand = "UPDATE [mytable] SET [color] = ? where [team] = ?" >
    > <UpdateParameters>
    > <asp:parameter Name="team" Type="String" />
    > <asp:parameter Name="color" Type="String" />
    >
    > <asp:parameter Name="original_team" Type="String" />
    > <asp:parameter Name="original_color" Type="String" />
    > </UpdateParameters>
    > </asp:SqlDataSource>
    >
    > <asp:GridView ID="GridView1" runat="server" DataKeyNames="field1"
    > DataSourceID="SqlDataSource1" >
    > <Columns>
    > <asp:CommandField ShowEditButton="True"/>
    > <asp:BoundField DataField="team" />
    > <asp:TemplateField HeaderText="color">
    > <ItemTemplate>
    > <asp:Label ID="kl" Runat="server" Text='<%# Bind("color")
    > %>'></asp:Label>
    > </ItemTemplate>
    >
    > <EditItemTemplate>
    > <asp:TextBox ID="kl" ReadOnly="True" Runat="server" Text='<%#dd%>'>
    > </asp:TextBox>
    > </EditItemTemplate>
    > </asp:TemplateField>
    > </Columns>
    > </asp:GridView>
    >
    > <asp:DropDownList ID="drd"
    > runat="server" AutoPostBack="True">
    > </asp:DropDownList>
    > ....
    >
    > code-behind:
    > -----------
    > friend dd as string
    > ...
    > Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    > Handles Me.Load
    > Dim ...
    > Dim ...
    > sConnectionString = "Provider = ...
    > sql = "SELECT [allcolor] FROM [colortable];"
    > d = New OleDbDataAdapter(sql, sConnectionString)
    > ds = New DataSet()
    > x = d.Fill(ds)
    > If Page.IsPostBack Then
    > dd = drd.SelectedValue
    > drd.Items.Clear()
    > End If
    > z = New ListItem("pick up a color", "pickup")
    > drd.Items.Add(z)
    > For i = 0 To x - 1
    > kl = ds.Tables(0).Rows(i).Item(0)
    > z = New ListItem(kl, kl)
    > drd.Items.Add(z)
    > Next
    > End Sub
    > End Class
     
    , Sep 4, 2006
    #2
    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. yamne
    Replies:
    1
    Views:
    1,679
    avnrao
    Jun 17, 2004
  2. =?Utf-8?B?ZGVuIDIwMDU=?=

    No Value: DropDownlist in EditItemTemplate Gridview Problem

    =?Utf-8?B?ZGVuIDIwMDU=?=, Oct 11, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    547
    =?Utf-8?B?ZGVuIDIwMDU=?=
    Oct 11, 2006
  3. Jeff
    Replies:
    0
    Views:
    396
  4. yamne
    Replies:
    0
    Views:
    153
    yamne
    Jun 17, 2004
  5. Ben

    strange problem with EditItemTemplate

    Ben, Aug 17, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    157
Loading...

Share This Page