C# 如何在GridView DropDownList中维护所选项目
我的问题是,在gridview\u行编辑事件之后,我无法在gridview中保留该项C# 如何在GridView DropDownList中维护所选项目,c#,asp.net,gridview,C#,Asp.net,Gridview,我的问题是,在gridview\u行编辑事件之后,我无法在gridview中保留该项 <asp:TemplateField HeaderStyle-CssClass="gridHeading" HeaderText="Get Alerts By SMS" ItemStyle-CssClass="gridValue" ItemStyle-HorizontalAlign="Center"> <Ite
<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();
}
}
受保护的无效grdGeofence\u行编辑(对象发送方,GridViewEditEventArgs e)
{
GridViewRow行=(GridViewRow)grdGeofence.Rows[e.NewEditIndex];
grdGeofence.EditIndex=e.NewEditIndex;
List geofenginedata=new blsmsalertdail().getgeofenginealertdetail(会话[“sessaccountid”].ToString(),txtdeviceID.Text);
对于(int y=0;y
数据库根据情况返回0/1,我使用布尔表达式将其转换为Yes和No
我想将“lblalertbyssgeofence”的值保留为dropdownlist“ddlalertbysmgeofence”
我看过很多网站的很多解决方案,包括SO。但是这个方法太长,也不符合我的情况。
我有大约100个下拉列表,我不能一次又一次地重写代码
有没有更简单的方法呢?为
下拉列表添加属性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>
在行数据绑定上,u可以执行此操作……但如果前端更改为u,以启用ddl的自动回写True并再次绑定网格视图,我可以执行此操作吗?在行编辑。。标签丢失,我无法检索其值。请尝试在gridview的RowDataBound
事件中设置下拉列表的值。@techdo用于每个下拉列表。。我是说100个下拉列表?为什么gridview绑定在事件grdGeofence\u行编辑中进行?是否有必要?选项所选值不可用。。看这个,这不是问题。。。默认情况下它不会出现。您只需添加该属性并进行检查。非常感谢您。。!!如果我能给你100+甜蜜的上帝之母。。!你让它变得如此简单。。!再次非常感谢。!我被卡住了。。