为什么我老是更新不了这个 dropdownlist所在gridview列 的值呢?
比如说 原来 ddlPianqu的值是 “福建”,我现在点编辑,把ddlPianqu的值改为“湖北”,然后点保存,结果ddlPianqu的值还是“福建”!
代码如下:
<asp:Button ID="outAdd" runat="Server" Text="添加新记录" OnClick="outAdd_Click" />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnDataBound="GridView1_RowDataBound"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" OnRowCommand="GridView1_OnRowCommand"
DataSourceID="SqlDataSource1" ForeColor="#333333" GridLines="None" OnRowDeleted="GridView1_RowDeleted"
DataKeyNames="id">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True"
CommandName="Update" Text="保存"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Cancel" Text="取消"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" CausesValidation="False"
CommandName="Edit" Text="编辑"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" Runat="server" Text="新增" OnClick="btnAdd_Click" />
<asp:Button ID="btnCancel" Runat="server" Text="隐藏" OnClick="btnCancel_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Select" Text="选择"></asp:LinkButton>
</ItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="btnDel" runat="server" CausesValidation="true" OnClientClick='javascript:return confirm("确定要删除吗?");'
CommandName="Delete" Text="删除"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="业务员" SortExpression="salesman">
<EditItemTemplate>
<asp:TextBox ID="salesmanTextBox" runat="server" Text='<%# Bind("salesman") %>'></asp:TextBox>
<asp:Label ID="lblId" Text='<%# Bind("id") %>' Visible="false" runat="server"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSalesman" runat="server" Text='<%# Bind("salesman") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="salesmanBox" Runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="片区">
<EditItemTemplate>
<asp:DropDownList ID="ddlPianqu" runat="server" AutoPostBack="true">
</asp:DropDownList>
<asp:Label ID="lblPianqu" Text='<%# Bind("pianquid") %>' Visible="false" runat="server"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:DropDownList ID="ddlPianqu" runat="server" AutoPostBack="true" Enabled="false">
</asp:DropDownList>
<asp:Label ID="lblPianqu" Text='<%# Bind("pianquid") %>' Visible="false" runat="server"></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlPianqu" runat="server" AutoPostBack="true">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:connHome %>"
DeleteCommand="DELETE FROM [salesman] WHERE [id] = @id"
InsertCommand="INSERT INTO [salesman] ([salesman], [pianquid]) VALUES (@salesman, @pianquid)"
SelectCommand="(SELECT [id], [salesman], [pianquid] FROM [salesman]) union (select 0 as [id],'开始标记' as [salesman],0 as [pianquid])"
UpdateCommand="UPDATE [salesman] SET [salesman] = @salesman, [pianquid] = @pianquid WHERE [id] = @id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="id" Type="Int32" />
<asp:Parameter Name="salesman" Type="String" />
<asp:Parameter Name="pianquid" Type="Int32" />
</UpdateParameters>
<InsertParameters>
<asp:Parameter Name="salesman" Type="String" />
<asp:Parameter Name="pianquid" Type="Int32" />
</InsertParameters>
</asp:SqlDataSource>
protected void GridView1_OnRowCommand(Object sender, GridViewCommandEventArgs e)
{
switch ()
{
case "Update":
ControlParameter cp_id = new ControlParameter("id", TypeCode.Int32, GridView1.Rows[GridView1.EditIndex].FindControl("lblId").UniqueID.ToString(), "Text");
ControlParameter cp_salesman = new ControlParameter("salesman", TypeCode.String, GridView1.Rows[GridView1.EditIndex].FindControl("salesmanTextBox").UniqueID.ToString(), "Text");
ControlParameter cp_pianquid = new ControlParameter("pianquid", TypeCode.Int32, GridView1.Rows[GridView1.EditIndex].FindControl("ddlPianqu").UniqueID.ToString(), "SelectedValue");
SqlDataSource1.UpdateParameters.Clear(); // 清理参数
SqlDataSource1.UpdateParameters.Add(cp_id);
SqlDataSource1.UpdateParameters.Add(cp_salesman);
SqlDataSource1.UpdateParameters.Add(cp_pianquid);
SqlDataSource1.Update();
break;
default:
break;
}
}