How to Pass Textbox value to SQLDataSource and Fire MessageBox based on Query Result?
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 r开发者_如何转开发unat="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
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.
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;
}
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
- remove the selectpara and you don't need the code behind
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
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>
精彩评论