3

My problem is I am not able to retain the item in the gridview after the Gridview_RowEditing event.

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

EDIT

    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();
        }
    }

The database return 0/1 depending upon the situation and I convert it into Yes and No using boolean expressions.

I want to keep the value of "lblAlertBySMSGeofence" as the selected text of the dropdownlist "ddlAlertBySMSGeofence"

I have seen many solution of many websites including SO. But the method are too lengthy and are also not in context with my situation. I have around 100 dropdownlists and I cannot rewrite the code again and again..

Is there a simpler way to do it.?

4

1 回答 1

3

SelectedValue='<%#Eval("alertBySMS")%>'为.添加属性DropDowList ddlAlertBySMSGeofence。检查链接如何在 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>
于 2013-04-18T07:35:36.700 回答