如何在 C# 中获取自定义 table 的数据集值
How to get the dataset values to custom table in c#
这里我试图将从数据库中检索到的值转换为我的自定义 table 格式。现在我正在将值存储到数据集。
protected void Page_Load(object sender, EventArgs e)
{
String userID = Convert.ToString(Session["user_id"]);
if (string.IsNullOrEmpty(userID) == true)
{
Response.Redirect("login.aspx");
}
try {
string scon="SERVER=localhost;DATABASE=bmtc;UID=root;";
MySqlConnection con = new MySqlConnection(scon);
String s = "select * from application";
MySqlDataAdapter dat = new MySqlDataAdapter(s, con);
DataSet ds = new DataSet();
dat.Fill(ds,"tbl");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
catch(Exception ex){
// Label1.Text = ex.ToString();
}
}
Asp代码:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
我需要在 table 中保存每个单独的值。
喜欢
<table class="custom-table" id="preview-table">
<tr><td>Name</td><td><% value1 %></td></tr>
<tr><td>Address</td><td><% value2 %></td></tr>
<tr><td>Phone</td><td><% value3 %></td></tr>
....
</table>
我该怎么做?
您可以使用listview
<asp:ListView ID="listview1" runat="server" ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
<table class="custom-table" id="preview-table">
<tr>
<th>
Header1
</th>
<th>
Header2
</th>
<th>
Header3
</th>
</tr>
<tr><asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<td>
<%# Eval("Column1") %>
</td>
<td>
<%# Eval("Column2") %>
</td>
<td>
<%# Eval("Column3") %>
</td>
</ItemTemplate>
</asp:ListView>
在服务器端代码:
protected void Page_Load(object sender, EventArgs e)
{
String userID = Convert.ToString(Session["user_id"]);
if (string.IsNullOrEmpty(userID) == true)
{
Response.Redirect("login.aspx");
}
try {
string scon="SERVER=localhost;DATABASE=bmtc;UID=root;";
MySqlConnection con = new MySqlConnection(scon);
String s = "select * from application";
MySqlDataAdapter dat = new MySqlDataAdapter(s, con);
DataSet ds = new DataSet();
dat.Fill(ds,"tbl");
listview1.DataSource = ds.Tables[0];
listview1.DataBind();
}
catch(Exception ex){
// Label1.Text = ex.ToString();
}
}
有关更多信息,请点击以下链接
如我的评论所述,ASP.NET Gridview
控件在浏览器中无论如何呈现为 Html table,因此他们无需定义自定义 table分开。现在,默认情况下,gridview 的 AutoGenerateColumns
属性 是 true
所以如果您简单地以编程方式绑定数据(就像您现在所做的那样),它将呈现 table.
如果您不想显示任何特定的列,那么要么不要从数据库中获取它,要么将 AutoGenerateColumns
属性 设置为 false
并为每个列添加 BoundField列。
尽管您当前的代码存在一些问题,但首先您应该只在初始获取请求时绑定数据,即不要在每次回发后绑定数据:-
protected void Page_Load(object sender, EventArgs e)
{
//code to get user_id
If(!PostBack)
{
//code to bind gridview.
}
}
除此之外,您应该将连接字符串存储在 Web.Config
文件中。您应该考虑使用 using statement 来处理昂贵的数据库相关对象。
这里我试图将从数据库中检索到的值转换为我的自定义 table 格式。现在我正在将值存储到数据集。
protected void Page_Load(object sender, EventArgs e)
{
String userID = Convert.ToString(Session["user_id"]);
if (string.IsNullOrEmpty(userID) == true)
{
Response.Redirect("login.aspx");
}
try {
string scon="SERVER=localhost;DATABASE=bmtc;UID=root;";
MySqlConnection con = new MySqlConnection(scon);
String s = "select * from application";
MySqlDataAdapter dat = new MySqlDataAdapter(s, con);
DataSet ds = new DataSet();
dat.Fill(ds,"tbl");
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
}
catch(Exception ex){
// Label1.Text = ex.ToString();
}
}
Asp代码:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
我需要在 table 中保存每个单独的值。 喜欢
<table class="custom-table" id="preview-table">
<tr><td>Name</td><td><% value1 %></td></tr>
<tr><td>Address</td><td><% value2 %></td></tr>
<tr><td>Phone</td><td><% value3 %></td></tr>
....
</table>
我该怎么做?
您可以使用listview
<asp:ListView ID="listview1" runat="server" ItemPlaceholderID="itemPlaceHolder1">
<LayoutTemplate>
<table class="custom-table" id="preview-table">
<tr>
<th>
Header1
</th>
<th>
Header2
</th>
<th>
Header3
</th>
</tr>
<tr><asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<td>
<%# Eval("Column1") %>
</td>
<td>
<%# Eval("Column2") %>
</td>
<td>
<%# Eval("Column3") %>
</td>
</ItemTemplate>
</asp:ListView>
在服务器端代码:
protected void Page_Load(object sender, EventArgs e)
{
String userID = Convert.ToString(Session["user_id"]);
if (string.IsNullOrEmpty(userID) == true)
{
Response.Redirect("login.aspx");
}
try {
string scon="SERVER=localhost;DATABASE=bmtc;UID=root;";
MySqlConnection con = new MySqlConnection(scon);
String s = "select * from application";
MySqlDataAdapter dat = new MySqlDataAdapter(s, con);
DataSet ds = new DataSet();
dat.Fill(ds,"tbl");
listview1.DataSource = ds.Tables[0];
listview1.DataBind();
}
catch(Exception ex){
// Label1.Text = ex.ToString();
}
}
有关更多信息,请点击以下链接
如我的评论所述,ASP.NET Gridview
控件在浏览器中无论如何呈现为 Html table,因此他们无需定义自定义 table分开。现在,默认情况下,gridview 的 AutoGenerateColumns
属性 是 true
所以如果您简单地以编程方式绑定数据(就像您现在所做的那样),它将呈现 table.
如果您不想显示任何特定的列,那么要么不要从数据库中获取它,要么将 AutoGenerateColumns
属性 设置为 false
并为每个列添加 BoundField列。
尽管您当前的代码存在一些问题,但首先您应该只在初始获取请求时绑定数据,即不要在每次回发后绑定数据:-
protected void Page_Load(object sender, EventArgs e)
{
//code to get user_id
If(!PostBack)
{
//code to bind gridview.
}
}
除此之外,您应该将连接字符串存储在 Web.Config
文件中。您应该考虑使用 using statement 来处理昂贵的数据库相关对象。