GridView和DropDownList 偶数行的DROPDOWNLIST不能绑定默认值,奇数行可以,代码如下
<%@ Page language="C#" %>
<%@ import namespace="System.Data" %><script runat="server">
void AuthorsGridView_RowDataBound (Object sender, GridViewRowEventArgs e)
{
// Check for a row in edit mode.
if(e.Row.RowState == DataControlRowState.Edit)
{
// Preselect the DropDownList control with the state value
// for the current row.
// Retrieve the underlying data item. In this example
// the underlying data item is a DataRowView object.
DataRowView rowView = (DataRowView)e.Row.DataItem;
// Retrieve the state value for the current row.
String state = rowView["state"].ToString();
// Retrieve the DropDownList control from the current row.
DropDownList list = (DropDownList)e.Row.FindControl("StatesList");
// Find the ListItem object in the DropDownList control with the
// state value and select the item.
ListItem item = list.Items.FindByText(state);
list.SelectedIndex = list.Items.IndexOf(item);
}
}
void AuthorsGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
// Retrieve the row being edited.
GridViewRow row = AuthorsGridView.Rows[AuthorsGridView.EditIndex];
// Retrieve the DropDownList control from the row.
DropDownList list = (DropDownList)row.FindControl("StatesList");
// Add the selected value of the DropDownList control to
// the NewValues collection. The NewValues collection is
// passed to the data source control, which then updates the
// data source.
e.NewValues["state"] = list.SelectedValue;
}
</script><html>
<body>
<form runat="server">
<h3>GridViewRow DataItem Example</h3> <asp:gridview id="AuthorsGridView"
datasourceid="AuthorsSqlDataSource"
autogeneratecolumns="false"
autogenerateeditbutton="true"
datakeynames="au_id"
onrowdatabound="AuthorsGridView_RowDataBound"
onrowupdating="AuthorsGridView_RowUpdating"
runat="server">
<columns>
<asp:boundfield datafield="au_lname"
headertext="Last Name"/>
<asp:boundfield datafield="au_fname"
headertext="First Name"/>
<asp:templatefield headertext="State">
<itemtemplate>
<%#Eval("state")%>
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist id="StatesList"
datasourceid="StatesSqlDataSource"
datatextfield="state"
runat="server"/>
<asp:sqldatasource id="StatesSqlDataSource"
selectcommand="SELECT Distinct [state] FROM [authors]"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
</edititemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Pubs sample database. -->
<asp:sqldatasource id="AuthorsSqlDataSource"
selectcommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors]"
updatecommand="UPDATE authors SET [au_lname]=@au_lname, [au_fname]=@au_fname, [state]=@state WHERE au_id=@au_id"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
<%@ Page language="C#" %>
<%@ import namespace="System.Data" %><script runat="server">
void AuthorsGridView_RowDataBound (Object sender, GridViewRowEventArgs e)
{
// Check for a row in edit mode.
if(e.Row.RowState == DataControlRowState.Edit)
{
// Preselect the DropDownList control with the state value
// for the current row.
// Retrieve the underlying data item. In this example
// the underlying data item is a DataRowView object.
DataRowView rowView = (DataRowView)e.Row.DataItem;
// Retrieve the state value for the current row.
String state = rowView["state"].ToString();
// Retrieve the DropDownList control from the current row.
DropDownList list = (DropDownList)e.Row.FindControl("StatesList");
// Find the ListItem object in the DropDownList control with the
// state value and select the item.
ListItem item = list.Items.FindByText(state);
list.SelectedIndex = list.Items.IndexOf(item);
}
}
void AuthorsGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
// Retrieve the row being edited.
GridViewRow row = AuthorsGridView.Rows[AuthorsGridView.EditIndex];
// Retrieve the DropDownList control from the row.
DropDownList list = (DropDownList)row.FindControl("StatesList");
// Add the selected value of the DropDownList control to
// the NewValues collection. The NewValues collection is
// passed to the data source control, which then updates the
// data source.
e.NewValues["state"] = list.SelectedValue;
}
</script><html>
<body>
<form runat="server">
<h3>GridViewRow DataItem Example</h3> <asp:gridview id="AuthorsGridView"
datasourceid="AuthorsSqlDataSource"
autogeneratecolumns="false"
autogenerateeditbutton="true"
datakeynames="au_id"
onrowdatabound="AuthorsGridView_RowDataBound"
onrowupdating="AuthorsGridView_RowUpdating"
runat="server">
<columns>
<asp:boundfield datafield="au_lname"
headertext="Last Name"/>
<asp:boundfield datafield="au_fname"
headertext="First Name"/>
<asp:templatefield headertext="State">
<itemtemplate>
<%#Eval("state")%>
</itemtemplate>
<edititemtemplate>
<asp:dropdownlist id="StatesList"
datasourceid="StatesSqlDataSource"
datatextfield="state"
runat="server"/>
<asp:sqldatasource id="StatesSqlDataSource"
selectcommand="SELECT Distinct [state] FROM [authors]"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
</edititemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the Pubs sample database. -->
<asp:sqldatasource id="AuthorsSqlDataSource"
selectcommand="SELECT [au_id], [au_lname], [au_fname], [state] FROM [authors]"
updatecommand="UPDATE authors SET [au_lname]=@au_lname, [au_fname]=@au_fname, [state]=@state WHERE au_id=@au_id"
connectionstring="server=localhost;database=pubs;integrated security=SSPI"
runat="server">
</asp:sqldatasource>
</form>
</body>
</html>
解决方案 »
- 向大家求助啊 有关asp.net中通过DataList排序问题
- finally里面的流关闭出错,大家帮忙,谢
- 50分关于GridView数据绑定问题
- 暴妖怪问题,所有成功的操作提示都有效,而所有失败的操作都提示都失效了??
- [求助] asp.net 程序 部署到 Windows2003 上 导出excel功能 hang住了
- **********请问11 30 2004 12:00AM这种类型的时间怎么转换啊*************
- 在TreeView中怎么才能让某个TreeNode不显示出来呢?(隐藏)
- 关于流媒体网站的开发`
- 我这几天写了个加强版的datagrid,提供单击双击行事件,提供水平/树直滚动条,滚动时表头固定
- 无法调试问题 (三日内结贴)
- GridView影响性能?
- 一个正则替换的问题!
{
if (((DropDownList)e.Row.FindControl("ddl")) != null)
{
DropDownList ddl = (DropDownList)e.Row.FindControl("ddl");
ddl.Items.Clear();
ddl.Items.Add(new ListItem("", "1")); ddl.SelectedValue = ((HiddenField)e.Row.FindControl("HD_ID")).Value;
}
} protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
//绑定数据
}
参考
我不知道原因出在什么地方?