C# GridView 控件绑定下拉列表框及给下拉列表框设定默认值
2009-08-19 16:38
477 查看
aspx文件内容
<form id="form1" runat="server" onsubmit="">
<div style="margin: 100px auto; width: 80%; text-align: left">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowUpdating"
OnRowUpdated="GridView1_RowUpdated" AutoGenerateColumns="False" DataKeyNames="pkID"
DataSourceID="SqlDataSource1" PageSize="200" Width="100%" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None">
<Columns>
<asp:BoundField DataField="pkID" HeaderText="pkID" InsertVisible="False" ReadOnly="True"
SortExpression="pkID" />
<asp:BoundField DataField="infoType" HeaderText="infoType" InsertVisible="False" ReadOnly="True"
SortExpression="infoType" />
<asp:BoundField DataField="title" HeaderText="标题" SortExpression="title" /><asp:BoundField DataField="webType" HeaderText="网站类型" SortExpression="webType" /><asp:BoundField DataField="remarkType" HeaderText="正负面评价" SortExpression="remarkType" /><asp:BoundField DataField="score" HeaderText="正负面评分" SortExpression="score" /><asp:BoundField DataField="pubTime" HeaderText="日期开始" SortExpression="pubTime" /><asp:BoundField DataField="endTime" HeaderText="日期结束" SortExpression="endTime" /><asp:BoundField DataField="replycount" HeaderText="相关文章数" SortExpression="replycount" />
<asp:BoundField DataField="link" HeaderText="链接" SortExpression="link" />
<asp:BoundField DataField="editor" HeaderText="编辑者" SortExpression="editor" />
<asp:TemplateField HeaderText="分类" >
<ItemTemplate>
<asp:DropDownList ID="ddlInfoType" runat="server" DataSourceID="SqlDataSource1" DataTextField="t_name"
DataValueField="pkid" >
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [t_name], [pkid] FROM [t_info_type]"></asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="" InsertCommand="" SelectCommand="" UpdateCommand="">
<DeleteParameters>
<asp:Parameter Name="pkID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
</div>
</form>
.cs文件代码
//红色字体部分用来设定下拉列表框默认值
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
int rid = Convert.ToInt32(((DataRowView)e.Row.DataItem)["infoType"]);
int Pkid = Convert.ToInt32(((DataRowView)e.Row.DataItem)["pkID"]);
string strLink = Convert.ToString(((DataRowView)e.Row.DataItem)["link"]);
((DataRowView)e.Row.DataItem).Row["title"] = "dddddd";
e.Row.Cells[2].Text =" <a href='http://blog.my400800.cn' target=\"_blank\">"+ e.Row.Cells[2].Text+"</a>";
DropDownList dd = (DropDownList)e.Row.FindControl("ddlInfoType");
// DataTable table = dd.DataSource as DataTable;
dd.SelectedValue = rid.ToString();
dd.Attributes.Add("onchange", "ajaxUpdateInfoType('" + Pkid + "',this);");
}
}
<form id="form1" runat="server" onsubmit="">
<div style="margin: 100px auto; width: 80%; text-align: left">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowUpdating"
OnRowUpdated="GridView1_RowUpdated" AutoGenerateColumns="False" DataKeyNames="pkID"
DataSourceID="SqlDataSource1" PageSize="200" Width="100%" BackColor="White" BorderColor="White" BorderStyle="Ridge" BorderWidth="2px" CellPadding="3" CellSpacing="1" GridLines="None">
<Columns>
<asp:BoundField DataField="pkID" HeaderText="pkID" InsertVisible="False" ReadOnly="True"
SortExpression="pkID" />
<asp:BoundField DataField="infoType" HeaderText="infoType" InsertVisible="False" ReadOnly="True"
SortExpression="infoType" />
<asp:BoundField DataField="title" HeaderText="标题" SortExpression="title" /><asp:BoundField DataField="webType" HeaderText="网站类型" SortExpression="webType" /><asp:BoundField DataField="remarkType" HeaderText="正负面评价" SortExpression="remarkType" /><asp:BoundField DataField="score" HeaderText="正负面评分" SortExpression="score" /><asp:BoundField DataField="pubTime" HeaderText="日期开始" SortExpression="pubTime" /><asp:BoundField DataField="endTime" HeaderText="日期结束" SortExpression="endTime" /><asp:BoundField DataField="replycount" HeaderText="相关文章数" SortExpression="replycount" />
<asp:BoundField DataField="link" HeaderText="链接" SortExpression="link" />
<asp:BoundField DataField="editor" HeaderText="编辑者" SortExpression="editor" />
<asp:TemplateField HeaderText="分类" >
<ItemTemplate>
<asp:DropDownList ID="ddlInfoType" runat="server" DataSourceID="SqlDataSource1" DataTextField="t_name"
DataValueField="pkid" >
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT [t_name], [pkid] FROM [t_info_type]"></asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="" InsertCommand="" SelectCommand="" UpdateCommand="">
<DeleteParameters>
<asp:Parameter Name="pkID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
</div>
</form>
.cs文件代码
//红色字体部分用来设定下拉列表框默认值
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRowView drv = e.Row.DataItem as DataRowView;
int rid = Convert.ToInt32(((DataRowView)e.Row.DataItem)["infoType"]);
int Pkid = Convert.ToInt32(((DataRowView)e.Row.DataItem)["pkID"]);
string strLink = Convert.ToString(((DataRowView)e.Row.DataItem)["link"]);
((DataRowView)e.Row.DataItem).Row["title"] = "dddddd";
e.Row.Cells[2].Text =" <a href='http://blog.my400800.cn' target=\"_blank\">"+ e.Row.Cells[2].Text+"</a>";
DropDownList dd = (DropDownList)e.Row.FindControl("ddlInfoType");
// DataTable table = dd.DataSource as DataTable;
dd.SelectedValue = rid.ToString();
dd.Attributes.Add("onchange", "ajaxUpdateInfoType('" + Pkid + "',this);");
}
}
相关文章推荐
- C# GridView 控件绑定下拉列表框及给下拉列表框设定默认值
- RadioButtonList控件绑定数据时设定默认值
- C# 程序中如何向(DEV)ComboBoxEdit控件中添加下拉列表的值不使用数据绑定,就是使用代码添加
- [C#]GridView操作子控件进行绑定同步
- 设定下拉列表控件DropDownList,复选按钮CheckBox,RadioButton默认值总结测试
- 设定下拉列表控件DropDownList,复选按钮CheckBox,RadioButton默认值总结测试
- C# 循环生成年份月份绑定到下拉列表框
- 设定下拉列表控件DropDownList,复选按钮CheckBox,RadioButton默认值总结测试
- winForm ComboBox 控件默认值绑定及只可选择不可输入设定处理
- 类库-----一些经常用到的对控件的处理1. 获取文本框中输入内容2.清除文本框内容3. 获取下拉列表框中选择内容,类型0为Text值,类型1为Value值4.DropDownList绑定日期
- winForm ComboBox 控件默认值绑定及只可选择不可输入设定处理
- RadioButtonList控件绑定数据时设定默认值
- ASP.NET中递归绑定DDL下拉控件
- C# WinForm控件开发如何设置属性的默认值
- [自定义服务器控件] 第二步:下拉列表框。
- 第十五讲 GridView数据绑定控件(二)
- .net c# 中获得GridView的EmptyDataTemplate中的控件的方法
- .net c# 中获得GridView的EmptyDataTemplate中的控件的方法
- 对下拉控件的绑定
- 使用c#制作的一个树形下拉控件