asp.net gridview编辑时,下拉框选择上默认的选项
前台:<asp:TemplateFieldHeaderText="所属类名"><ItemTemplate><asp:LabelID="Label3"runat="serv...
前台: <asp:TemplateField HeaderText="所属类名">
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("firName") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="dfirst" runat="server">
</asp:DropDownList>
</EditItemTemplate>
我想在点击修改后,这个下拉框默认选择的项是与数据库中的数值对应的,不知道该如何实现。我的后台代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(e.Row.RowState == DataControlRowState.Edit ||e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
path = "select firId,secId from video_Video where vId=" + GridView1.DataKeys[e.Row.RowIndex].Value.ToString() +"";
re = sqldo.GetRow(path);
if (re.Read())
{
int firID = Convert.ToInt16(re["firId"].ToString());
re.Close();
//一级类绑定
DropDownList list = (DropDownList)e.Row.FindControl("dfirst");
path = "select * from video_FirstClass";
list.DataSource = sqldo.GetRows(path, "a");
list.DataTextField = "firName";
list.DataValueField = "firId";
list.DataBind();
list.SelectedIndex = Convert.ToInt16(re["firId"].ToString());
sqlcon.SqlClose();
} 展开
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("firName") %>' ></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="dfirst" runat="server">
</asp:DropDownList>
</EditItemTemplate>
我想在点击修改后,这个下拉框默认选择的项是与数据库中的数值对应的,不知道该如何实现。我的后台代码:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(e.Row.RowState == DataControlRowState.Edit ||e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
path = "select firId,secId from video_Video where vId=" + GridView1.DataKeys[e.Row.RowIndex].Value.ToString() +"";
re = sqldo.GetRow(path);
if (re.Read())
{
int firID = Convert.ToInt16(re["firId"].ToString());
re.Close();
//一级类绑定
DropDownList list = (DropDownList)e.Row.FindControl("dfirst");
path = "select * from video_FirstClass";
list.DataSource = sqldo.GetRows(path, "a");
list.DataTextField = "firName";
list.DataValueField = "firId";
list.DataBind();
list.SelectedIndex = Convert.ToInt16(re["firId"].ToString());
sqlcon.SqlClose();
} 展开
4个回答
展开全部
把list.SelectedIndex = Convert.ToInt16(re["firId"].ToString());
改成
if(!string.IsNullOrEmpty(re["firId"].ToString()))
{
list.SelectedValue = re["firId"].ToString();
}
判断一下值是否为空,为空可能会报错!
改成
if(!string.IsNullOrEmpty(re["firId"].ToString()))
{
list.SelectedValue = re["firId"].ToString();
}
判断一下值是否为空,为空可能会报错!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
list.SelectedIndex = Convert.ToInt16(re["firId"].ToString());
改成
list.SelectedValue = re["firId"].ToString();
改成
list.SelectedValue = re["firId"].ToString();
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
创建一个 RowUpdate时间
protected void GV_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
最好还是在这里做啊!
protected void GV_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
}
最好还是在这里做啊!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不行找我
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(e.Row.RowState == DataControlRowState.Edit ||e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
path = "select firId,secId from video_Video where vId=" + GridView1.DataKeys[e.Row.RowIndex].Value.ToString() +"";
re = sqldo.GetRow(path);
if (re.Read())
{
int firID = Convert.ToInt16(re["firId"].ToString());
re.Close();
//一级类绑定
DropDownList list = (DropDownList)e.Row.FindControl("dfirst");
path = "select * from video_FirstClass";
list.DataSource = sqldo.GetRows(path, "a");
list.DataTextField = "firName";
list.DataValueField = "firId";
list.DataBind();
list.SelectedValue = firID.ToString();
sqlcon.SqlClose();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(e.Row.RowState == DataControlRowState.Edit ||e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
path = "select firId,secId from video_Video where vId=" + GridView1.DataKeys[e.Row.RowIndex].Value.ToString() +"";
re = sqldo.GetRow(path);
if (re.Read())
{
int firID = Convert.ToInt16(re["firId"].ToString());
re.Close();
//一级类绑定
DropDownList list = (DropDownList)e.Row.FindControl("dfirst");
path = "select * from video_FirstClass";
list.DataSource = sqldo.GetRows(path, "a");
list.DataTextField = "firName";
list.DataValueField = "firId";
list.DataBind();
list.SelectedValue = firID.ToString();
sqlcon.SqlClose();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询