如何根据查询结果将文本框值传递给 SQLDataSource 和 Fire MessageBox?

发布于 2024-12-08 17:48:31 字数 1956 浏览 2 评论 0原文

我创建了一个简单的表单,其中包含一个提示用户输入 ID 的文本框。表单上有一个提交按钮和一个将文本框值连接为查询参数的 SQLDataSource。

<fieldset>
<asp:Label runat="server" ID="lblAcctNo">Please enter the account number:</asp:Label>
<asp:TextBox runat="server" ID="txtAcctNo"></asp:TextBox>
<asp:Button runat="server" ID="btnSubmit" Text="Submit" />
</fieldset>

<asp:SqlDataSource ID="sdsMMSPts" runat="server" 
ConnectionString="<%$ ConnectionStrings:mmsptsConnectionString %>" 

SelectCommand="SELECT [Recommended], [PatientId] FROM [Patients] WHERE ([PatientId] = @PatientId)">
<SelectParameters>
    <asp:ControlParameter ControlID="txtAcctNo" Name="PatientId" 
        PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>

我见过在代码隐藏中建立 SQl 连接的示例。这是必要的吗,还是上面的 SQLDataSource 可以解决这个问题?任何人都可以分享一些基于查询结果创建消息框的好链接吗?就我而言,如果推荐为 1,则需要显示消息 1,如果推荐为 2,则需要显示消息 2。如果用户 ID 无效,则需要显示第三条消息。我该如何实现这个目标?

更新:
在我的代码隐藏中,到目前为止我已经编写了以下内容:

protected void Page_Load(object sender, EventArgs e)
    {
        string connString = System.Configuration.ConfigurationManager.ConnectionStrings
        ["mmsptsConnectionString"].ConnectionString;

        SqlConnection sqlconn = new SqlConnection(connString);
        sqlconn.Open();

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = connString;
        cmd.CommandType = SqlDataSourceCommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter ("@PatientId", SQLDbType.varchar ));

        using (var reader = cmd.ExecuteReader()) 
        {while reader.Read()
                    txtAcctNo.Text=cmd.Parameters("@PatientId")
        }

    }

我的目的是调用我的存储过程并将 binindg @PatientId 之后的结果返回到 txtAcctNo.Text。我对 SqlClient 的东西有点生疏,需要采取哪些进一步步骤才能从我的存储过程中获取结果?输出参数之一是@Recommended,它是一个位字段。我将如何评估参数值并根据此参数的值触发消息框警报或标签控件?例如,如果@Recommended = 1,则将“消息1”输出到label1.text,否则将“消息2”输出到lable1.text

I've created a simple form that contains a textbox prompting the user to enter an ID. There's a submit button on the form and a SQLDataSource that wires up the textbox value as a parameter for the query.

<fieldset>
<asp:Label runat="server" ID="lblAcctNo">Please enter the account number:</asp:Label>
<asp:TextBox runat="server" ID="txtAcctNo"></asp:TextBox>
<asp:Button runat="server" ID="btnSubmit" Text="Submit" />
</fieldset>

<asp:SqlDataSource ID="sdsMMSPts" runat="server" 
ConnectionString="<%$ ConnectionStrings:mmsptsConnectionString %>" 

SelectCommand="SELECT [Recommended], [PatientId] FROM [Patients] WHERE ([PatientId] = @PatientId)">
<SelectParameters>
    <asp:ControlParameter ControlID="txtAcctNo" Name="PatientId" 
        PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>

I've seen examples where a SQl connection is established in codebehind. Is that necessary, or does the above SQLDataSource take care of that? Can anyone share some good links to creating message boxes based on query results? In my case, if Recommended is 1 then messaage 1 would need to be show, if Reccommended is 2 then message 2 would need to be shown. If the UserID is invalid, the third message needs to be shown. How do I accomplish this?

UPDATE:
In my codebehind, I've written the following thus far:

protected void Page_Load(object sender, EventArgs e)
    {
        string connString = System.Configuration.ConfigurationManager.ConnectionStrings
        ["mmsptsConnectionString"].ConnectionString;

        SqlConnection sqlconn = new SqlConnection(connString);
        sqlconn.Open();

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = connString;
        cmd.CommandType = SqlDataSourceCommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter ("@PatientId", SQLDbType.varchar ));

        using (var reader = cmd.ExecuteReader()) 
        {while reader.Read()
                    txtAcctNo.Text=cmd.Parameters("@PatientId")
        }

    }

My intent here is to call my stored procedure and return the results after binindg @PatientId to the txtAcctNo.Text. I'm a bit rusty on the SqlClient stuff, what further steps are needed to get the results from my stored procedure? One of the output parametsrs is @Recommended which is a bit field. How would I evaluate the parameter value and trigger either a message box alert or a label control based on the value of this paramenter? For example, if @Recommended = 1 then output "Message 1" to label1.text else output "Message 2" to lable1.text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

要走就滚别墨迹 2024-12-15 17:48:31

您必须在代码隐藏中完成这项工作,无论是使用 SqlDataSource 还是直接使用 SqlConnection 都取决于您。但是 SqlDataSource 无法直接与 javascript 或 javascript 库交互。

You'll have to do that work in the code-behind, whether you use the SqlDataSource or use SqlConnection directly is up to you. But the SqlDataSource has no ability to directly interact with javascript or javascript libraries.

月下凄凉 2024-12-15 17:48:31

您可以尝试使用OnSelecting事件来修改参数:

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    e.Command.Parameters["Arg1"].Value = TextBox1.Text;
}

You can try using the OnSelecting event to modify the parameters:

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    e.Command.Parameters["Arg1"].Value = TextBox1.Text;
}
云朵有点甜 2024-12-15 17:48:31

这是一个非常晚的响应,但对于那些仍然关注 @ this 的人来说,

这是为了展示如何将 Texbox Value 绑定到数据源并在修改文本框值后自动刷新它

  1. 删除 selectpara 并且您不需要
  2. 只是 后面的代码将 AutoPostBack="True" 设置为文本框,以便当用户在其上键入内容然后退出时(或在本例中添加一个虚拟搜索按钮 btnSubmit 并将 autopostback 设置为 true),它将触发并且数据源将刷新

  3. 修改数据源selectcommand 通过删除病人 ID 的 where 子句并为其添加过滤器:

SelectCommand="SELECT [推荐], [PatientId] 来自 [患者]" FilterExpression="PatientId = '{0}'"

     <FilterParameters>
        <asp:ControlParameter Name="PatientId" ControlID="txtAcctNo" />
    </FilterParameters>   

This is a very late response but for those who still look @ this

This is to show how to bind Texbox Value to the datasource and autorefresh it after modifying the textbox value

  1. remove the selectpara and you don't need the code behind
  2. just set AutoPostBack="True" to the textbox so when user type on it then exit from it ( or add a dummy search button in this case btnSubmit and set autopostback to true) it will fire and the datasource will refresh

  3. modify the datasource selectcommand by removing the where clause of the patientid and add filter to it:

SelectCommand="SELECT [Recommended], [PatientId] FROM [Patients]" FilterExpression="PatientId = '{0}'"

     <FilterParameters>
        <asp:ControlParameter Name="PatientId" ControlID="txtAcctNo" />
    </FilterParameters>   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文