2011-12-01 66 views
0

pulldownlist中pull selectedvalue/item所以我有一个gridview中的下拉列表,我试图从选择值来更新我的数据库。我的问题是:当按下更新按钮时,它会忽略为选定值选择的内容,并抓取首次加载下拉列表时第一个值设置为的内容。无法从更新

所以我的问题是:在gridview的更新事件中,如何从dropdownlist中获取选定的值。

下面你会发现我的codebehind和gridview的标记。

代码隐藏:

/// <summary> 
/// Handles the Click event of the update button under edit in the gridview control. 
/// </summary> 
/// <param name="sender">The source of the event.</param> 
/// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewUpdateEventArgs"/> instance containing the event data.</param> 
protected void GridViewHolder_Updating(object sender, GridViewUpdateEventArgs e) 
{ 
    int machineid; 
    string machineid1; 
    string machineTypeid; 
    string machineModelid; 

    //retrieve and set the data 
    GridViewHolder.EditIndex = e.RowIndex; 
    BindData(); 

    GridViewRow row = (GridViewRow)GridViewHolder.Rows[e.RowIndex]; 
    TextBox mID = row.FindControl("MachineIDText") as TextBox; 
    DropDownList mType = row.FindControl("MachineTypeDropDown") as DropDownList; 
    DropDownList mModel = row.FindControl("MachineModelDropDown") as DropDownList; 

    machineid1 = mID.Text; 
    machineid = Convert.ToInt32(machineid1); 
    machineTypeid = mType.SelectedValue; 
    machineModelid = mModel.SelectedValue; 

    try 
    { 
     if (machineTypeid != "empty" || machineModelid != "empty") 
     { 
      if (machineTypeid != "empty") 
      { 
       inputsService.UpdateMachineTypes(machineid, machineTypeid); 
      } 
      if (machineModelid != "empty") 
      { 
       inputsService.UpdateMachineModels(machineid, machineModelid); 
      } 
      UpdateSucceed.Visible = true; 
      logger.Debug("Updating - Database successfully updated!"); 
     } 
     else 
     { 
      UpdateFail.Visible = true; 
      logger.Debug("Updating - Database had no data selected to be updated."); 
     } 
    } 
    catch (Exception ex) 
    { 
     logger.ErrorFormat("Updating - Failed to update the table, ex = {0}", ex); 
    } 
} 

/// <summary> 
/// Handles the Click event of the cancel button under edit in the gridview control. 
/// </summary> 
/// <param name="sender">The source of the event.</param> 
/// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewCancelEditEventArgs"/> instance containing the event data.</param> 
protected void GridViewHolder_Canceling(object sender, GridViewCancelEditEventArgs e) 
{ 
    //reset the edit index 
    GridViewHolder.EditIndex = -1; 
    //Bind data to GridViewHolder 
    BindData(); 
} 

protected void GridViewHolder_DataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (this.GridViewHolder.EditIndex != -1) 
    { 
     DropDownList mType = e.Row.FindControl("MachineTypeDropDown") as DropDownList; 
     DropDownList mModel = e.Row.FindControl("MachineModelDropDown") as DropDownList; 
    } 
} 

protected void GridViewHolder_Editing(object sender, GridViewEditEventArgs e) 
{ 
    //set the edit index to a new value 
    GridViewHolder.EditIndex = e.NewEditIndex; 
    //Bind data to gridviewholder 
    BindData(); 
} 

#endregion 

#region Private Methods 

private void BindData() 
{ 
    GridViewHolder.DataSource = Session["MachineTable"]; 
    GridViewHolder.DataBind(); 
} 

gridview的标记:

<asp:GridView ID="GridViewHolder" 
         runat="server" 
         AllowPaging="True" 
         AutoGenerateColumns="False" 
         BackColor="Transparent" 
         BorderColor="#999999" 
         BorderStyle="Ridge" 
         BorderWidth="3px" 
         CellPadding="4" 
         CellSpacing="2" 
         DataSourceID="MachineDataSet" 
         ForeColor="Black" 
         HeaderStyle-HorizontalAlign="Center" 
         HorizontalAlign="Center" 
         RowStyle-HorizontalAlign="Center" 
         Width="796px" 
         OnRowUpdating="GridViewHolder_Updating" 
         OnRowCancelingEdit="GridViewHolder_Canceling" 
         OnRowEditing="GridViewHolder_Editing" 
         OnRowDataBound="GridViewHolder_DataBound"       
         EnableViewState="False"> 
      <RowStyle BackColor="Transparent" 
         HorizontalAlign="Center" /> 
      <Columns> 
       <asp:TemplateField HeaderText="ID" 
            SortExpression="ID" 
            Visible="False"> 
        <ItemTemplate> 
         <asp:Label ID="MachineIDLabel" 
            runat="server" 
            Text='<%# Bind("ID") %>' 
            Visible="false"></asp:Label> 
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:TextBox ID="MachineIDText" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox> 
        </EditItemTemplate> 
       </asp:TemplateField> 
       <asp:BoundField DataField="SiteName" 
           HeaderText="Site Name" 
           SortExpression="SiteName" 
           ReadOnly="true" /> 
       <asp:BoundField DataField="Name" 
           HeaderText="Machine Name" 
           ReadOnly="true" 
           SortExpression="Name" /> 
       <asp:TemplateField HeaderText="Machine Type" 
            SortExpression="MachineType"> 
        <ItemTemplate> 
         <asp:Label ID="MachineTypeLabel" 
            runat="server" 
            Text='<%# Bind("MachineType") %>'> 
         </asp:Label>        
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:DropDownList ID="MachineTypeDropDown" 
              runat="server" 
              AppendDataBoundItems="True"             
              Height="21px" 
              Width="217px" 
              DataSourceID="GetMachineType" 
              DataTextField="Name" 
              DataValueField="ID"> 
          <asp:ListItem Enabled="true" 
              Text="Select a Machine Type." 
              Value="empty"> 
          </asp:ListItem> 
         </asp:DropDownList> 
        </EditItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Machine Model" SortExpression="MachineModel"> 
        <ItemTemplate> 
         <asp:Label ID="MachineModelLabel" 
            runat="server" 
            Text='<%# Bind("MachineModel") %>'> 
         </asp:Label>        
        </ItemTemplate> 
        <EditItemTemplate> 
         <asp:DropDownList ID="MachineModelDropDown" 
              runat="server" 
              AppendDataBoundItems="True"             
              Height="21px" Width="217px" 
              DataSourceID="GetMachineModel" 
              DataTextField="Name" 
              DataValueField="ID"> 
          <asp:ListItem Enabled="true" 
              Text="Select a Machine Model." 
              Value="empty"> 
          </asp:ListItem> 
         </asp:DropDownList> 
        </EditItemTemplate> 
       </asp:TemplateField> 
       <asp:CommandField ButtonType="Button" 
            ShowEditButton="True" 
            CausesValidation="false" > 
        <ItemStyle HorizontalAlign="Center" 
           Wrap="True" /> 
       </asp:CommandField> 
      </Columns> 
      <FooterStyle BackColor="Transparent" /> 
      <PagerStyle BackColor="Transparent" 
         ForeColor="Black" 
         HorizontalAlign="Left" /> 
      <SelectedRowStyle BackColor="Transparent" 
           Font-Bold="True" 
           ForeColor="White" /> 
      <HeaderStyle BackColor="Black" 
         Font-Bold="True" 
         ForeColor="White" 
         HorizontalAlign="Center" /> 
    </asp:GridView> 

任何帮助或建议,我们非常欢迎,

谢谢

+0

你有viewstate关闭在你的gridview,它是否有所作为,如果你打开它? – ipr101

+0

@ ipr101当我有viewstate打开它抛出,尝试运行取消或更新按钮在编辑模式 – James213

回答

2

GridViewUpdateEventArgs型的RowUpdating event活动论点。这包含关键字,新旧值的字典。没有必要通过在GridViewRow上使用FindControl来获取它们。

但您获得旧值的主要原因是您在此事件中再次进行DataBinding。然后用DataSource中的值覆盖所有更改。

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridviewupdateeventargs.aspx

更新:似乎DropDownListSelectedValue不是NewValues字典的一部分。因此,您可以将其添加到RowUpdating事件中。

protected void GridViewHolder_Updating(object sender, GridViewUpdateEventArgs e) 
{ 
    GridViewRow row = (GridViewRow)GridViewHolder.Rows[e.RowIndex]; 
    DropDownList mType = row.FindControl("MachineTypeDropDown") as DropDownList; 
    e.NewValues.Add("MachineType", mType.SelectedValue): 
} 
+0

好吧,但无法加载视图状态,但如果我删除数据绑定形式更新事件我最终无法找到下拉列表作为控件。我将如何去找到更新中的ddl而不运行更新中的数据绑定? – James213

+0

在读完新值之后,您应该将DataBind **绑定到DataBind **。看到这个例子:http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating.aspx –

+0

好吧,但我的问题是:没有该数据绑定之前搜索控件该页面无法找到任何控件,从而引发空引用错误,无法读取任何值,更不用说新值。 – James213