OnClick Run SQL Code Output to Specific DIV
Basically when a user clicks a button I have a javascript code that runs that displays the hidden DIV, my question is how do I make it so wh开发者_开发知识库en the users click it executes the SQL code (which is already written to select the data) and then it displays it in the DIV
that I want. Would the only way to do this be add a label to the DIV and then use LabelName.Text
to populate it or can this be accomplished some other way? I am using Visual Studio 2008 and coding in c#
First you have to decide if you want to populate the content using JavaScript (and AJAX) or via postback to the server using ASP.NET's code behind design.
Postback is easier (but not quite as good a user experience depending on connection speed.) To do this you need to make an event handler for the buttons click event and then change the contents of the page in C#. There are many examples of this and any book on ASP.NET will describe it. Search for "Changing page contents via button click C#", or something similar. (edit: or look at @Nikhil's answer -- he gives a good skeleton of how to do it.)
AJAX is harder. Here you will need to define a API web service to return the data you want and then code in JavaScript to call the service and render the results. There are many JavaScript libraries that can make this easier -- jQuery is very accessible and will even has working examples of this very functionality on their documentation page.
You can use update panels to achieve this easily.
<asp:UpdatePanel id="testPanel" runat="server" UpdateMode="Conditional" UseChildrenAsTriggers="false">
<Triggers>
<AsynchPostBackTrigger ControlId="btnExecute"/>
</Triggers>
<ContentTemplate>
<div id="divout" runat="server" visible="false"/>
<asp:Button id="btnExecute" runat="server" onclick="btnExecute_Click"/>
</ContentTemplate>
</asp:UpdatePanel>
//Then in your codebehind
protected void btnExecute_Click(object sender, EventArgs e){
//do SQL Processign here
divout.InnerHtml = "desired text";
divout.Visible=true;
}
精彩评论