| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 664 人关注过本帖
标题:为什么我老是更新不了这个 dropdownlist所在gridview列 的值呢?
只看楼主 加入收藏
micccn
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2008-8-19
结帖率:0
收藏
 问题点数:0 回复次数:5 
为什么我老是更新不了这个 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>
                       &nbsp;<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" /> &nbsp;
                       <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>&nbsp;
                   </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;
      }
    }
搜索更多相关主题的帖子: False server asp 福建 
2012-05-08 17:09
micccn
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2008-8-19
收藏
得分:0 
自己顶一下!菜鸟求救!  

[ 本帖最后由 micccn 于 2012-5-9 11:36 编辑 ]

淘宝振东在线电脑出租
2012-05-09 10:22
micccn
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2008-8-19
收藏
得分:0 
再顶!

淘宝振东在线电脑出租
2012-05-11 08:28
micccn
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2008-8-19
收藏
得分:0 
不顶不行啊!  

淘宝振东在线电脑出租
2012-05-11 12:57
qq947485421
Rank: 2
等 级:论坛游民
帖 子:5
专家分:15
注 册:2012-4-23
收藏
得分:0 
用的都是自带的保存,你DropDownList里面数据“福建”是从哪里读取的?而且pianquid"类型是int,大致看了下页面
2012-05-14 12:58
micccn
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2008-8-19
收藏
得分:0 
DropDownList 的数据是这么来的:CreateDDlPianqu();
    protected void CreateDDlPianqu()
    {
      DropDownList ddl = new DropDownList();
      OleDbConnection conn1 = new OleDbConnection(q0.Conn.GetConnString());
      System.Data.OleDb.OleDbDataAdapter adpt1 = new System.Data.OleDb.OleDbDataAdapter("select distinct [id],[pianqu] from pianqu", conn1);
      DataTable q = new DataTable();
      adpt1.Fill(q);

      for (int i = 0; i < GridView1.Rows.Count; i++)
      {
        ddl = (DropDownList)GridView1.Rows[i].FindControl("ddlPianqu");
        ddl.Items.Clear();
        if (q != null && q.Rows.Count > 0)
        {
          foreach (DataRow r in q.Rows)
          {
            ddl.Items.Add(new ListItem(HttpUtility.HtmlDecode((string)r["pianqu"]), ((Int32)r["id"]).ToString()));
          }
        }
      }
      ddl = (DropDownList)GridView1.FooterRow.FindControl("ddlPianqu");
      ddl.Items.Clear();
      if (q != null && q.Rows.Count > 0)
      {
        foreach (DataRow r in q.Rows)
        {
          ddl.Items.Add(new ListItem(HttpUtility.HtmlDecode((string)r["pianqu"]), ((Int32)r["id"]).ToString()));
        }
      }
      conn1.Close();
    }

"pianquid"的类型是int没错,但是无论我代码写为:
ControlParameter cp_pianquid = new ControlParameter("pianquid", TypeCode.String, GridView1.Rows[GridView1.EditIndex].FindControl("ddlPianqu").UniqueID.ToString(), "SelectedValue");
还是:
ControlParameter cp_pianquid = new ControlParameter("pianquid", TypeCode.Int32, GridView1.Rows[GridView1.EditIndex].FindControl("ddlPianqu").UniqueID.ToString(), "SelectedValue");
都不行,
跟踪一下,GridView1.Rows[GridView1.EditIndex].FindControl("ddlPianqu")的SelectedValue值对应的是 “湖北”,但cp_pianquid的值,怎么DefaultValue是null
不知怎么回事。。。

淘宝振东在线电脑出租
2012-05-16 11:31
快速回复:为什么我老是更新不了这个 dropdownlist所在gridview列 的值呢?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029476 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved