C# 从数据库中调用数据并在按钮函数中使用它
C# Calling data from DB and using it in a Button Function
基本上我有一个存储系统信息的数据库
- 系统 ID
- 系统名称
- 系统描述
- 系统路径
现在系统路径将由管理员使用与该系统相关的 aspx 页面的路径填充。
我的问题是,我的 SqlDataSource table 旁边是否可以有一个与我的数据库中的系统相关的按钮,单击该按钮将采用系统路径并转到它,所以如果您单击 System1 的查看按钮,您将被定向到 ~/pages/system1.aspx
--- 错误 ---
[HttpException (0x80004005): Multiple controls with the same ID 'LinkButton1' were found. FindControl requires that controls have unique IDs.]
System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection controls) +233
System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection controls) +311
System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +304
System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +412
System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +412
System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +412
System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +412
System.Web.UI.Page.FindControl(String id) +38
System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +245
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1960
是的,这是可能的。以下是按钮逻辑的细分:
点击事件:
- 获取当前选择的记录(例如调用:SystemRecord)
- 导航到路径
Response.Redirect(SystemRecord.System Path)
我假设 SystemId
是主键。这个想法是当您单击 View
时获取该行的 SystemId
以将其传递给您要重定向到的 url。
使用 gridview 的 aspx 页面:
<asp:Label runat="server" ID="lblMessage" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowSorting="True" AllowPaging="True" PageSize="10" EmptyDataText="No Data Found" DataKeyNames="SystemId" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="ID" SortExpression="SystemId">
<ItemTemplate>
<asp:Label ID="lblSystemId" runat="server" Text='<%# Eval("SystemId") />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="SystemName">
<ItemTemplate>
<asp:Label ID="lblName" Text='<%# Eval("SystemName") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="SystemDescription">
<ItemTemplate>
<asp:Label ID="lblDescription" Text='<%# Eval("SystemDescription") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="btnView" runat="server" CommandArgument='<%# Eval("SystemId")%>'
ToolTip="Click to view this system" OnClick="lbtView_Click">View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:YOUR-CONNECTIONSTRING-IN-WEB.CONFIG %>" SelectCommand="SELECT * FROM SYSTEM"></asp:SqlDataSource>
后面的代码:
protected void lbtView_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
try
{
LinkButton lbtView = (LinkButton)sender;
string SystemID = lbtView.CommandArgument;
Response.Redirect("/pages" + SystemID + ".aspx");
}
catch (Exception ex)
{
//Exeption message
lblMessage.Text = "An error has occurred. Please try again later!</br><i>" + ex.Message + "</i>";
}
}
else
{
lblMessage.Text = "Page validation failed. Please try again!";
}
}
基本上我有一个存储系统信息的数据库
- 系统 ID
- 系统名称
- 系统描述
- 系统路径
现在系统路径将由管理员使用与该系统相关的 aspx 页面的路径填充。
我的问题是,我的 SqlDataSource table 旁边是否可以有一个与我的数据库中的系统相关的按钮,单击该按钮将采用系统路径并转到它,所以如果您单击 System1 的查看按钮,您将被定向到 ~/pages/system1.aspx
--- 错误 ---
[HttpException (0x80004005): Multiple controls with the same ID 'LinkButton1' were found. FindControl requires that controls have unique IDs.]
System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection controls) +233
System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection controls) +311
System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +304
System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +412
System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +412
System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +412
System.Web.UI.Control.FindControl(String id, Int32 pathOffset) +412
System.Web.UI.Page.FindControl(String id) +38
System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad) +245
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1960
是的,这是可能的。以下是按钮逻辑的细分:
点击事件:
- 获取当前选择的记录(例如调用:SystemRecord)
- 导航到路径
Response.Redirect(SystemRecord.System Path)
我假设 SystemId
是主键。这个想法是当您单击 View
时获取该行的 SystemId
以将其传递给您要重定向到的 url。
使用 gridview 的 aspx 页面:
<asp:Label runat="server" ID="lblMessage" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" AllowSorting="True" AllowPaging="True" PageSize="10" EmptyDataText="No Data Found" DataKeyNames="SystemId" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField HeaderText="ID" SortExpression="SystemId">
<ItemTemplate>
<asp:Label ID="lblSystemId" runat="server" Text='<%# Eval("SystemId") />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="SystemName">
<ItemTemplate>
<asp:Label ID="lblName" Text='<%# Eval("SystemName") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="SystemDescription">
<ItemTemplate>
<asp:Label ID="lblDescription" Text='<%# Eval("SystemDescription") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="btnView" runat="server" CommandArgument='<%# Eval("SystemId")%>'
ToolTip="Click to view this system" OnClick="lbtView_Click">View</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:YOUR-CONNECTIONSTRING-IN-WEB.CONFIG %>" SelectCommand="SELECT * FROM SYSTEM"></asp:SqlDataSource>
后面的代码:
protected void lbtView_Click(object sender, EventArgs e)
{
if (Page.IsValid)
{
try
{
LinkButton lbtView = (LinkButton)sender;
string SystemID = lbtView.CommandArgument;
Response.Redirect("/pages" + SystemID + ".aspx");
}
catch (Exception ex)
{
//Exeption message
lblMessage.Text = "An error has occurred. Please try again later!</br><i>" + ex.Message + "</i>";
}
}
else
{
lblMessage.Text = "Page validation failed. Please try again!";
}
}