Using C# with ASP开发者_JAVA百科.NET, how do I show a "success" message when my user submits a form? And at the same time say "The image has successfully saved", with a link, so that the image created can be viewed by clicking the link?
Wrap your form in an <asp:Panel>
and create another <asp:Panel>
with Visible="False"
for your Thank you message. Once the form is submitted, change the visibility of each panel, setting the form to Visible="False"
, and the thank you message panel to Visible="True"
.
Hope that makes sense, here's an example:
<asp:Panel ID="pnlFormFields" runat="server">
... form fields here ...
</asp:Panel>
<asp:Panel ID="pnlThankYouMessage" runat="server" Visible="False">
... Thank you message here ...
</asp:Panel>
Then inside your codebehind
protected void btnSubmit_Click(object sender, EventArgs e) {
// Hook up uploaded image and assign link to it
pnlFormFields.Visible = false;
pnlThankYouMessage.Visible = true;
}
If you need label to display message. Add a label on the page and set its attribute visible = false in aspx and use the code below:
protected void btnSubmit_Click(object sender, EventArgs e) {
if(SaveRecordsToDataDatabase())
{
If(UploadImage())
{
showMessage("Save successfull",true);
}
else
{
showMessage("Save failed",false);
}
}
else
{
showMessage("Save failed",false);
}
}
private bool UploadImage()
{
// you upload image code..
}
private bool SaveRecordsToDatabase()
{
// db save code
}
private void showMessage(string message, bool success)
{
lblMsg.visible = true; // here lblMsg is asp label control on your aspx page.
lblMsg.FontBold = true;
if(success)
lblMsg.ForeColor = Color.Green;
else
lblMsg.ForeColor = Color.Green;
lblMsg.Text = message;
}
For consistency you can use Transaction in above code so as to prevent save operation if image upload fails. Otherwise its your choice. The new code with Transaction will be , given below:
protected void btnSubmit_Click(object sender, EventArgs e) {
using(TransactionScope scope = new TransactionScope())
{
if(SaveRecordsToDataDatabase())
{
If(UploadImage())
{
showMessage("Save successfull",true);
}
else
{
showMessage("Save failed",false);
}
}
else
{
showMessage("Save failed",false);
}
}
scope.complete()
}
Here to refer transaction scope, add reference System.Transactions.
İf you want to show message on client side controls, like alert("saccess"); you can use ajax and webmethod in Why doesn't my jQuery code work in Firefox and Chrome? if you want to show message on server side you can use panel, label or div (runat server and have id) and default setting of them, set visiible false, when you show message you can set visible true via code behind..
use a label(visible=false) and a hyperlink from the toolbox .when u upload an image u must be inserting the url of savefile location to a database.so when that insert query is fired that will return an integer value which is d no of lines inserted in db.compare it like if that value > 0 then set visibily of label to true and label.text="success" finally set the navigate url of the hyperlink to d url of saved image which can be used to creat a view link of the image
精彩评论