Home All Groups Group Topic Archive Search About

strange problem with EditItemTemplate

Author
17 Aug 2006 1:41 PM
Ben
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

Author
4 Sep 2006 2:41 PM
rayfusion
Hi Ben, I am having the same issue. Did you ever figure this one out?

Ben wrote:

Show quoteHide quote
> 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