例如我点击ID=3的一条记录,然后按编辑如何在相应姓名字段绑定一个下拉列表框,可提供数据选择呢?而且怎让它显示为当前ID为3内容呢?
14 个解决方案
#1
第一种办法
protected void DropDownList2_SelectedIndexChanged1(object sender, EventArgs e)
{
int cc = GridView1.Rows.Count;
for (int i = 0; i < cc; i++)
{
//DropDownList ddl = GridView1.Rows[i].Cells[14].FindControl("DropDownList2") as DropDownList;
}
DropDownList ddl = GridView1.Rows[0].Cells[14].FindControl("DropDownList2") as DropDownList;
//string aa = ddl.SelectedItem.Text;
string bb = GridView1.Rows.Count.ToString();
//DropDownList ddlProvince = (DropDownList)sender;
//DataListItem dl = (DataListItem)ddlProvince.Parent;
//DataGridColumn dgc = (DataGridColumn)dl.Parent;
DropDownList drp = sender as DropDownList; // 触发事件的 DropDownList
int a = Convert.ToInt32(drp.SelectedValue);
GridViewRow row = drp.NamingContainer as GridViewRow; // GridView 当前行
// row.Style.Add(HtmlTextWriterStyle.BackgroundColor, drp.SelectedValue);
int CurRow = row.RowIndex;
string aa = GridView1.Rows[CurRow].Cells[a].Text;
RegisterStartupScript("", "<script>alert('" + aa + "');</script>");
}
第二种办法
function test(obj) {
var a=obj.value;
alert(a);
if (a!="--请选择--") {
alert( obj.parentNode.parentNode.childNodes.item(a).innerHTML );
}
}
protected void DropDownList2_SelectedIndexChanged1(object sender, EventArgs e)
{
int cc = GridView1.Rows.Count;
for (int i = 0; i < cc; i++)
{
//DropDownList ddl = GridView1.Rows[i].Cells[14].FindControl("DropDownList2") as DropDownList;
}
DropDownList ddl = GridView1.Rows[0].Cells[14].FindControl("DropDownList2") as DropDownList;
//string aa = ddl.SelectedItem.Text;
string bb = GridView1.Rows.Count.ToString();
//DropDownList ddlProvince = (DropDownList)sender;
//DataListItem dl = (DataListItem)ddlProvince.Parent;
//DataGridColumn dgc = (DataGridColumn)dl.Parent;
DropDownList drp = sender as DropDownList; // 触发事件的 DropDownList
int a = Convert.ToInt32(drp.SelectedValue);
GridViewRow row = drp.NamingContainer as GridViewRow; // GridView 当前行
// row.Style.Add(HtmlTextWriterStyle.BackgroundColor, drp.SelectedValue);
int CurRow = row.RowIndex;
string aa = GridView1.Rows[CurRow].Cells[a].Text;
RegisterStartupScript("", "<script>alert('" + aa + "');</script>");
}
第二种办法
function test(obj) {
var a=obj.value;
alert(a);
if (a!="--请选择--") {
alert( obj.parentNode.parentNode.childNodes.item(a).innerHTML );
}
}
#2
我怎样把数据绑定企下拉上在GV显示呢?
#3
我不知道GridView是如何显示的,我说下在DataList如何实现吧.
首先在EditItemTemplate中隐藏所需要的控件:
<EditItemTemplate>
<tr>
<td>
<asp:ImageButton ID="btnSave" runat="server" ImageUrl="~/resource/image/icon/ico-save_16x16.png"
ToolTip="更新" CommandName="Update" />
</td>
</tr>
</EditItemTemplate>
然后点编辑按钮后显示这个控件(这个时候肯定是触发编辑事件),这个时候得到该列的ID,通过ID得到所需的数据,然后绑定到控件中就OK了.
还有啥问题可以M下我.
首先在EditItemTemplate中隐藏所需要的控件:
<EditItemTemplate>
<tr>
<td>
<asp:ImageButton ID="btnSave" runat="server" ImageUrl="~/resource/image/icon/ico-save_16x16.png"
ToolTip="更新" CommandName="Update" />
</td>
</tr>
</EditItemTemplate>
然后点编辑按钮后显示这个控件(这个时候肯定是触发编辑事件),这个时候得到该列的ID,通过ID得到所需的数据,然后绑定到控件中就OK了.
还有啥问题可以M下我.
#4
<ItemTemplate>
<asp:DropDownList ID="ddlSort" runat="server" Width="98%">
</asp:DropDownList>
<asp:HiddenField ID="Hd_Sort" runat="server" Value='<%# Eval("BalSort")%>' />
</ItemTemplate>
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{ if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
((DropDownList)e.Row.FindControl("ddlSort")).SelectedValue = ((HiddenField)e.Row.FindControl("Hd_Sort")).Value;
}
}
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
string strSort = ((DropDownList)gv.Rows[e.NewEditIndex].FindControl("ddlSort")).SelectedValue.Trim();
gv.EditIndex = -1;
BindData();
}
<asp:DropDownList ID="ddlSort" runat="server" Width="98%">
</asp:DropDownList>
<asp:HiddenField ID="Hd_Sort" runat="server" Value='<%# Eval("BalSort")%>' />
</ItemTemplate>
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{ if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
((DropDownList)e.Row.FindControl("ddlSort")).SelectedValue = ((HiddenField)e.Row.FindControl("Hd_Sort")).Value;
}
}
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
string strSort = ((DropDownList)gv.Rows[e.NewEditIndex].FindControl("ddlSort")).SelectedValue.Trim();
gv.EditIndex = -1;
BindData();
}
#5
<asp:TemplateField HeaderText="学校" >
<ItemTemplate>
<asp:Label ID="lblSchool1" runat="server" style="display:none;" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlSchool2" DataSource='<%# (DataTable)ViewState["table"] %>' SelectedValue='<%# Eval("ID") %>' DataTextField="NAME" DataValueField="ID" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
在后台要将dropwoanlist的数据源放在ViewState中
<ItemTemplate>
<asp:Label ID="lblSchool1" runat="server" style="display:none;" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlSchool2" DataSource='<%# (DataTable)ViewState["table"] %>' SelectedValue='<%# Eval("ID") %>' DataTextField="NAME" DataValueField="ID" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
在后台要将dropwoanlist的数据源放在ViewState中
#6
mark..
#7
那怎样在后台将dropwoanlist的数据源放在ViewState?
兄台详细说说
#8
按4楼办法
RowDataBound 事件, 里面你想干嘛干嘛
在里面你可以取到该行的数据表里任何一个值
找到那个控件, 赋值即可,很简单。
学会用 RowDataBound 事件,你会发现 GridView绑定数据 没有完成不了的任务!
RowDataBound 事件, 里面你想干嘛干嘛
在里面你可以取到该行的数据表里任何一个值
找到那个控件, 赋值即可,很简单。
学会用 RowDataBound 事件,你会发现 GridView绑定数据 没有完成不了的任务!
#9
能不能举个例子啊?
#10
不要把数据随便仍在ViewState上。数据写在当前页面的一个局部变量上,然后再 Eval 表达式中使用这个局部变量就行了。
#11
不要丢随便数据到ViewState。
#12
晕了我问题没有根解决!谁能搞个详细例子?
#13
再来人讲讲啊?
#14
在EditItemTemplate标签里面加入DropDownList。直接数据绑定在DropDownList里面就OK。
#1
第一种办法
protected void DropDownList2_SelectedIndexChanged1(object sender, EventArgs e)
{
int cc = GridView1.Rows.Count;
for (int i = 0; i < cc; i++)
{
//DropDownList ddl = GridView1.Rows[i].Cells[14].FindControl("DropDownList2") as DropDownList;
}
DropDownList ddl = GridView1.Rows[0].Cells[14].FindControl("DropDownList2") as DropDownList;
//string aa = ddl.SelectedItem.Text;
string bb = GridView1.Rows.Count.ToString();
//DropDownList ddlProvince = (DropDownList)sender;
//DataListItem dl = (DataListItem)ddlProvince.Parent;
//DataGridColumn dgc = (DataGridColumn)dl.Parent;
DropDownList drp = sender as DropDownList; // 触发事件的 DropDownList
int a = Convert.ToInt32(drp.SelectedValue);
GridViewRow row = drp.NamingContainer as GridViewRow; // GridView 当前行
// row.Style.Add(HtmlTextWriterStyle.BackgroundColor, drp.SelectedValue);
int CurRow = row.RowIndex;
string aa = GridView1.Rows[CurRow].Cells[a].Text;
RegisterStartupScript("", "<script>alert('" + aa + "');</script>");
}
第二种办法
function test(obj) {
var a=obj.value;
alert(a);
if (a!="--请选择--") {
alert( obj.parentNode.parentNode.childNodes.item(a).innerHTML );
}
}
protected void DropDownList2_SelectedIndexChanged1(object sender, EventArgs e)
{
int cc = GridView1.Rows.Count;
for (int i = 0; i < cc; i++)
{
//DropDownList ddl = GridView1.Rows[i].Cells[14].FindControl("DropDownList2") as DropDownList;
}
DropDownList ddl = GridView1.Rows[0].Cells[14].FindControl("DropDownList2") as DropDownList;
//string aa = ddl.SelectedItem.Text;
string bb = GridView1.Rows.Count.ToString();
//DropDownList ddlProvince = (DropDownList)sender;
//DataListItem dl = (DataListItem)ddlProvince.Parent;
//DataGridColumn dgc = (DataGridColumn)dl.Parent;
DropDownList drp = sender as DropDownList; // 触发事件的 DropDownList
int a = Convert.ToInt32(drp.SelectedValue);
GridViewRow row = drp.NamingContainer as GridViewRow; // GridView 当前行
// row.Style.Add(HtmlTextWriterStyle.BackgroundColor, drp.SelectedValue);
int CurRow = row.RowIndex;
string aa = GridView1.Rows[CurRow].Cells[a].Text;
RegisterStartupScript("", "<script>alert('" + aa + "');</script>");
}
第二种办法
function test(obj) {
var a=obj.value;
alert(a);
if (a!="--请选择--") {
alert( obj.parentNode.parentNode.childNodes.item(a).innerHTML );
}
}
#2
我怎样把数据绑定企下拉上在GV显示呢?
#3
我不知道GridView是如何显示的,我说下在DataList如何实现吧.
首先在EditItemTemplate中隐藏所需要的控件:
<EditItemTemplate>
<tr>
<td>
<asp:ImageButton ID="btnSave" runat="server" ImageUrl="~/resource/image/icon/ico-save_16x16.png"
ToolTip="更新" CommandName="Update" />
</td>
</tr>
</EditItemTemplate>
然后点编辑按钮后显示这个控件(这个时候肯定是触发编辑事件),这个时候得到该列的ID,通过ID得到所需的数据,然后绑定到控件中就OK了.
还有啥问题可以M下我.
首先在EditItemTemplate中隐藏所需要的控件:
<EditItemTemplate>
<tr>
<td>
<asp:ImageButton ID="btnSave" runat="server" ImageUrl="~/resource/image/icon/ico-save_16x16.png"
ToolTip="更新" CommandName="Update" />
</td>
</tr>
</EditItemTemplate>
然后点编辑按钮后显示这个控件(这个时候肯定是触发编辑事件),这个时候得到该列的ID,通过ID得到所需的数据,然后绑定到控件中就OK了.
还有啥问题可以M下我.
#4
<ItemTemplate>
<asp:DropDownList ID="ddlSort" runat="server" Width="98%">
</asp:DropDownList>
<asp:HiddenField ID="Hd_Sort" runat="server" Value='<%# Eval("BalSort")%>' />
</ItemTemplate>
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{ if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
((DropDownList)e.Row.FindControl("ddlSort")).SelectedValue = ((HiddenField)e.Row.FindControl("Hd_Sort")).Value;
}
}
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
string strSort = ((DropDownList)gv.Rows[e.NewEditIndex].FindControl("ddlSort")).SelectedValue.Trim();
gv.EditIndex = -1;
BindData();
}
<asp:DropDownList ID="ddlSort" runat="server" Width="98%">
</asp:DropDownList>
<asp:HiddenField ID="Hd_Sort" runat="server" Value='<%# Eval("BalSort")%>' />
</ItemTemplate>
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowIndex != -1)
{ if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
((DropDownList)e.Row.FindControl("ddlSort")).SelectedValue = ((HiddenField)e.Row.FindControl("Hd_Sort")).Value;
}
}
}
protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
{
gv.EditIndex = e.NewEditIndex;
string strSort = ((DropDownList)gv.Rows[e.NewEditIndex].FindControl("ddlSort")).SelectedValue.Trim();
gv.EditIndex = -1;
BindData();
}
#5
<asp:TemplateField HeaderText="学校" >
<ItemTemplate>
<asp:Label ID="lblSchool1" runat="server" style="display:none;" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlSchool2" DataSource='<%# (DataTable)ViewState["table"] %>' SelectedValue='<%# Eval("ID") %>' DataTextField="NAME" DataValueField="ID" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
在后台要将dropwoanlist的数据源放在ViewState中
<ItemTemplate>
<asp:Label ID="lblSchool1" runat="server" style="display:none;" Text='<%# Eval("ID") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddlSchool2" DataSource='<%# (DataTable)ViewState["table"] %>' SelectedValue='<%# Eval("ID") %>' DataTextField="NAME" DataValueField="ID" runat="server">
</asp:DropDownList>
</EditItemTemplate>
</asp:TemplateField>
在后台要将dropwoanlist的数据源放在ViewState中
#6
mark..
#7
那怎样在后台将dropwoanlist的数据源放在ViewState?
兄台详细说说
#8
按4楼办法
RowDataBound 事件, 里面你想干嘛干嘛
在里面你可以取到该行的数据表里任何一个值
找到那个控件, 赋值即可,很简单。
学会用 RowDataBound 事件,你会发现 GridView绑定数据 没有完成不了的任务!
RowDataBound 事件, 里面你想干嘛干嘛
在里面你可以取到该行的数据表里任何一个值
找到那个控件, 赋值即可,很简单。
学会用 RowDataBound 事件,你会发现 GridView绑定数据 没有完成不了的任务!
#9
能不能举个例子啊?
#10
不要把数据随便仍在ViewState上。数据写在当前页面的一个局部变量上,然后再 Eval 表达式中使用这个局部变量就行了。
#11
不要丢随便数据到ViewState。
#12
晕了我问题没有根解决!谁能搞个详细例子?
#13
再来人讲讲啊?
#14
在EditItemTemplate标签里面加入DropDownList。直接数据绑定在DropDownList里面就OK。