2013-04-18 34 views
3

我的問題是我無法在Gridview_RowEditing事件後的gridview中保留該項目。如何維護GridView中的選定項目DropDownList

<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根據具體情況,我把它轉換成是與使用布爾表達式沒有。

我想保持的"lblAlertBySMSGeofence"值,因爲我看到很多網站,包括做的許多解決方案的下拉列表"ddlAlertBySMSGeofence"

的選定文本。但是這個方法太冗長了,也不符合我的情況。 我有大約100個dropdownlists,我不能一次又一次地重寫代碼。

有沒有更簡單的方法來做到這一點。

+0

上排數據綁定U可以做到這一點....如果變化對前端妳比再次啓用DDL和綁定網格視圖的自動回真 – 2013-04-18 06:42:14

+0

何找我有事嗎?在行編輯中..標籤會丟失我無法檢索其值.. – writeToBhuwan 2013-04-18 06:46:46

+0

嘗試在GridView的RowDataBound事件中設置下拉列表的值。 – TechDo 2013-04-18 06:47:43

回答

2

DropDowList添加屬性SelectedValue='<%#Eval("alertBySMS")%>'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> 
+0

選項選擇的值不是可用..看到這個http://i.imgur.com/B2ClDAU.jpg – writeToBhuwan 2013-04-18 07:43:48

+0

這不是一個問題......它不會默認來。您只需添加該屬性並檢查。 – TechDo 2013-04-18 07:45:12

+0

哦,上帝,你救了我.. !!!!!! – writeToBhuwan 2013-04-18 07:50:52

相關問題