【问题标题】:C# Calling data from DB and using it in a Button FunctionC#从数据库调用数据并在按钮函数中使用它
【发布时间】:2015-09-23 11:57:42
【问题描述】:

基本上我有一个存储系统信息的数据库

  1. 系统 ID
  2. 系统名称
  3. 系统说明
  4. 系统路径

现在系统路径将由管理员填写与该系统相关的 aspx 页面的路径。

我的问题是,我的 SqlDataSource 表旁边是否可以有一个与我的数据库中的系统相关的按钮,单击该按钮将采用系统路径并转到它,所以如果您单击 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

【问题讨论】:

  • 你的问题能不能说的更清楚一些
  • 请正确格式化问题..我们无法清楚地说明。
  • 目前还不清楚您从这个问题中提出的问题。假设管理员填写的路径是用“系统路径”查询的,以检索具有该路径的所有系统是否正确?那么你到底需要这个按钮做什么呢?
  • @Harvey 基本上管理员将输入与该系统相关的 aspx 页面的路径,以便用户可以通过单击“查看”按钮访问系统页面
  • 为什么你有多个按钮?您只需要一个名为“查看..”的按钮。用户可以选择记录以引用正确的“系统路径”

标签: c# asp.net database webforms


【解决方案1】:

是的,这是可能的。以下是按钮逻辑的细分:

点击事件:

  • 获取当前选定的记录(例如,调用:SystemRecord)
  • 导航到路径Response.Redirect(SystemRecord.System Path)

【讨论】:

  • 你能用代码解释一下我是如何做到这一点的吗?
  • 如果不了解您的设计,这将很难。但是您引用了 SQLDataSourceTable 的“SelectedItem”属性。这将为您提供当前选择的记录。在此处之后,您可以引用记录的字段属性。即系统路径
  • 检查我对问题所做的编辑...我现在得到了那个错误
【解决方案2】:

我假设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!";
            }
        }

【讨论】:

  • 我现在收到此错误 第 27 行: 第 28 行: 第 29 行:查看​​ton> 第 31 行:跨度>
  • 编译器错误消息:CS1061:“ASP.pages_viewsystems_aspx”不包含“lbtView_Click”的定义,并且找不到接受“ASP.pages_viewsystems_aspx”类型的第一个参数的扩展方法“lbtView_Click”(您是否缺少 using 指令或程序集引用?)
  • 我没有完全测试我的代码,但我的想法是使用 CommandArgument 来获取 SystemID,请将所有代码发布在您的 .aspx 页面中,然后我们可以修复它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 2020-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多