c# - 如何维护 GridView DropDownList 中的选定项

标签 c# asp.net gridview

我的问题是我无法在 Gridview_RowEditing 事件后将项目保留在 gridview 中。

 <asp:TemplateField HeaderStyle-CssClass="gridHeading" HeaderText="Get Alerts By SMS"
                                ItemStyle-CssClass="gridValue" ItemStyle-HorizontalAlign="Center">
         <ItemTemplate>
                 <asp:Label ID="lblAlertBySMSGeofence" runat="server" Text=' <%# (Convert.ToBoolean(Convert.ToInt32(Eval("alertBySMS")))) ? "Yes" : "No" %>'></asp:Label>
         </ItemTemplate>
         <EditItemTemplate>
              <asp:DropDownList ID="ddlAlertBySMSGeofence" runat="server" AppendDataBoundItems="true"
                                        CssClass="gridValue">
                                        <asp:ListItem Text="Yes" Value="1"/>
                                        <asp:ListItem Text="No" Value="0" />
                                    </asp:DropDownList>
                                </EditItemTemplate>
                                <HeaderStyle CssClass="gridHeading" />
                                <ItemStyle CssClass="gridValue" HorizontalAlign="Center" />
 </asp:TemplateField>

编辑

    protected void grdGeofence_RowEditing(object sender, GridViewEditEventArgs e)
    {

        GridViewRow row = (GridViewRow)grdGeofence.Rows[e.NewEditIndex];
        grdGeofence.EditIndex = e.NewEditIndex;
        List<COMMONGeofenceAlert> geofenceData = new BLsmsalertdetail().getGeofencealertDetail(Session["sessaccountid"].ToString(), txtdeviceID.Text);
        for (int y = 0; y < geofenceData.Count; y++)
        {
            geofenceData[y].vehicleNumber = ddlVehicleNumber.SelectedItem.Text;
        }
        grdGeofence.DataSource = geofenceData;
        grdGeofence.DataBind();
    }

protected void grdGeofence_RowUpdating(object sender, GridViewUpdateEventArgs e)
 {
    GridViewRow row = (GridViewRow)grdGeofence.Rows[e.RowIndex];

    string id = grdGeofence.DataKeys[e.RowIndex].Value.ToString();

    Label lblVehicle = (Label)row.FindControl("lblVehicleGeofence");
    TextBox mobileNumber = (TextBox)row.FindControl("txtMobileGeofence");
    TextBox EmailID = (TextBox)row.FindControl("txtEmailGeofence");
    DropDownList ddlAlertBySMS = (DropDownList)row.FindControl("ddlAlertBySMSGeofence");
    DropDownList ddlAlertbyEmail = (DropDownList)row.FindControl("ddlAlertByeEmailGeofence");
    DropDownList AlertAtGeofenceEnter = (DropDownList)row.FindControl("ddlAlertGeofenceEnter");
    DropDownList alertAtGeofenceExit = (DropDownList)row.FindControl("ddlAlertGeofenceExit");
    DropDownList ddlAddress = (DropDownList)row.FindControl("ddlGeofenceAddressGrid");
    BLsmsalertdetail detail = new BLsmsalertdetail();

    int i = updateGeofence(id, mobileNumber.Text, EmailID.Text, ddlAlertBySMS.SelectedItem.Value, ddlAlertbyEmail.SelectedItem.Value, AlertAtGeofenceEnter.SelectedItem.Value, alertAtGeofenceExit.SelectedItem.Value, ddlAddress.SelectedItem.Text);
    if (i == 1)
    {
        ScriptManager.RegisterClientScriptBlock(this.Page, typeof(Page), Guid.NewGuid().ToString(), "alert('Success!!')", true);
    }
    else
    {
        ScriptManager.RegisterClientScriptBlock(this.Page, typeof(Page), Guid.NewGuid().ToString(), "alert('Error!! Could not Update the value.')", true);
        }

        grdGeofence.EditIndex = -1;
        List<COMMONGeofenceAlert> geofenceData = new BLsmsalertdetail().getGeofencealertDetail(Session["sessaccountid"].ToString(), txtdeviceID.Text);
        for (int y = 0; y < geofenceData.Count; y++)
        {
            geofenceData[y].vehicleNumber = ddlVehicleNumber.SelectedItem.Text;
        }
        grdGeofence.DataSource = geofenceData;
        grdGeofence.DataBind();
}

protected void grdGeofence_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (grdGeofence.EditIndex == e.Row.RowIndex && e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList ddlAddress = (DropDownList)e.Row.Cells[0].FindControl("ddlGeofenceAddressGrid");
            List<COMMONsmsalertdetail> getSmsDispatcherData = new BLsmsalertdetail().getSMSalertDetail(Session["sessaccountid"].ToString());

            for (int i = 0; i < getSmsDispatcherData.Count; i++)
            {
                ddlAddress.Items.Add(new ListItem(getSmsDispatcherData[i].place, getSmsDispatcherData[i].place));
       }
            //ddlAddress.DataSource = getSmsDispatcherData;
            //ddlAddress.DataTextField = "place";
            //ddlAddress.DataValueField = "place";
            //ddlAddress.DataBind();
        }
    }

数据库根据情况返回 0/1,我使用 bool 表达式将其转换为 Yes 和 No。

我想保留 "lblAlertBySMSGeofence" 的值作为下拉列表的选定文本 "ddlAlertBySMSGeofence"

我看过很多网站的很多解决方案,包括SO。但是这个方法太长了,也不符合我的情况。 我有大约 100 个下拉列表,我不能一次又一次地重写代码。

有没有更简单的方法呢?

最佳答案

添加属性 SelectedValue='<%#Eval("alertBySMS")%>'对于 DropDowList ddlAlertBySMSGeofence .检查链接How to set SelectedValue of DropDownList in GridView EditTemplate了解更多详情。

<EditItemTemplate>
  <asp:DropDownList ID="ddlAlertBySMSGeofence" runat="server"
      AppendDataBoundItems="true" SelectedValue='<%#Eval("alertBySMS")%>'  
      CssClass="gridValue">
         <asp:ListItem Text="Yes" Value="1" />
         <asp:ListItem Text="No" Value="0" />
</asp:DropDownList>

关于c# - 如何维护 GridView DropDownList 中的选定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16076087/

相关文章:

java - 在Android编程中,如何在不使用XML的情况下用Java制作GridView?

c# - 多用户环境中的静态变量与公共(public)变量

c# - 如果没有图表,如何在 Azure 中设计 SQL 数据库?

c# - 从用户控件访问母版页属性

c# - 递归方法不会循环 child 的 child

Android:如何创建类似 Facebook 的图片库网格?

c# - 设计抽象基类。使用什么类型,抽象的还是具体的?

c# - 如何在 oData .Net core 3.1 中启用 $levels

asp.net - 使用 MVC 在 ASP.NET 中实现 API 的最佳方法是什么?

.net - Visual Studio 2010 - WPF/Silverlight 和内置网格