如何在 GridView 的下拉列表框中保留所选项目

3

我的问题是在GridView_RowEditing事件后无法保留网格视图中的项。

 <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,我使用布尔表达式将其转换为是和否。

我希望将"ddlAlertBySMSGeofence"下拉列表中的选定文本值保留为"lblAlertBySMSGeofence"

我已经在许多网站上看到了许多解决方案,包括SO。但这些方法过于繁琐,并且与我的情况不相关。我有大约100个下拉列表,我不能一遍又一遍地重写代码...

是否有更简单的方法?


在行数据绑定时,您可以这样做......但如果在前端更改,则需要将ddl的AutoPostback属性设置为True,并重新绑定网格视图。 - Amit Singh
我该怎么做?在行编辑时,标签丢失了,我无法检索其值。 - writeToBhuwan
尝试在gridview的RowDataBound事件中设置下拉列表的值。 - TechDo
对于每个下拉菜单..我的意思是100个下拉菜单吗? - writeToBhuwan
当GridView行处于编辑模式时,您不希望<EditItemTemplate>中的文本框具有值吗? - writeToBhuwan
显示剩余2条评论
1个回答

3

DropDownList ddlAlertBySMSGeofence 添加属性 SelectedValue='<%#Eval("alertBySMS")%>'。查看链接如何在GridView EditTemplate中设置DropDownList的SelectedValue了解更多细节。

<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>

选定的选项值不可用。请参见此链接http://i.imgur.com/B2ClDAU.jpg。 - writeToBhuwan
这不是问题... 它不会默认出现。你只需要添加那个属性并进行检查。 - TechDo
非常感谢您!!如果我能给您100+的话 - writeToBhuwan
天啊!你把它变得如此简单了!再次感谢你!我之前卡住了。 - writeToBhuwan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接