Implementing Delete Confirmation Dialog in List View (ASP.Net) – Part 2
In my previous blog post I described how to implementing delete confirmation dialog in ListView Control of ASP.Net. In one comment Dan Gilleland asked how to get the localized message text from resource file. In this post I am providing solution to Dan’s query.
I have created a project which has following files:
1. default.aspx (and other related files)
2. XMLFile.xml (a simple xml file which provides the data to list view control)
3. Resource1.resx (resource file that contains the localized string.
Follow the below steps to get the confirm dialog text from resource file.
1. Create a resource file in App_GlobalReosurces folder. I named this file as Resource1.
2. Add a string in the Reaource1.resx. I named it as DeleteConfirm. Enter the value you want to display on the delete confirmation dialog.
3. Open your code behind file (.aspx.cs). Write the following line in the Page_Load method.
public partial class _Default : System.Web.UI.Page { protected string deleteConfirmText; protected void Page_Load(object sender, EventArgs e) { deleteConfirmText = App_GlobalResources.Resource1.ResourceManager.GetString("DeleteConfirm"); } }
Note that I declared a string variable outside the method and made it protected. This makes the deleteConfirmText variable visible in the .aspx file.
4. Create a javascript function in the body of your aspx page.
<scriptlanguage="javascript">
function delConf() {
return"<%= deleteConfirmText%>";
}
</script>
5. Change the OnClientClickevent of delete button to call JavaScript function.
<asp:LinkButton ID="DeleteButton" runat="server" CommandName="Delete" ForeColor="Red" Text="Delete" OnClientClick="return confirm(delConf());" />
6. Save all and run your page.
Downloads
You can download the complete project from here.