Editing a gridview cell using Modal which is shown using rel attribute - javascript

I open a (css made)modal dialog using the rel="#showEditModal" attribute of a button.
I used it to add values to database then update gridview and got it working at last.
Problem is now i need to edit this values when clicked upon in the gridview.
It is something like this:
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="BtnEditDep" rel="#showEditModal" CssClass="btn showModal"
runat="server" CausesValidation="false" Text="Test" OnClientClick="window.scrollTo =
function(x,y) { return true; };" />
</ItemTemplate>
</asp:TemplateField>
This pretty much screws me because i need to send a variable(ID) to that modal
1) If i put it inside the Grid_RowCommand it won't go there until after the modal shows up
tried putting the value inside a viewstate => can't seem to be able to extract the value from the gridview
2) Tried to open the modal dynamically by adding attribute ("rel","#showEditModal") dynamically INSIDE the Grid_RowCommand but apparently I will need to click it again to show the modal (looked into programmatically clicking the button but failed)
3) Also tried it like this(didn't work - couldn't send parameter)
OnClientClick="SaveValue('<%= (BtnEditDep.ClientID).Text %>);
window.scrollTo = function(x,y) { return true; };"
....
function SaveValue(name)
{
$("hiddenelement").value = "<%= (BtnEditDep.ClientID).Text %>";
}
How can i accomplish this because I am currently stumped.
Thanks.

I think you should try to pack your function of saving data and refreshing gridview in a service and use ajax to fire it on client side, that's the most nature solution for your problem.

Related

asp.net partial postback with update panels

I am trying to have an update panel show when i click on a button. Right now it makes a postback of the whole page and it takes a long time to get all the information as the first time. I am trying to speed up that time and would like to know if there was a way to click the button show a panel over top of the page with the rest of the page grayed out and not postback. If I set onClientClick="return false" then the update panel never shows.
<asp:ImageButton ID="DetailButton" runat="server" CommandName="detail" CommandArgument='<%# Eval("id") %>'
ImageUrl="~/Images/detailbutton_export.png" onmouseover="this.src='/Images/detailbutton2_export.png'"
onmouseout="this.src='/Images/detailbutton_export.png'" />
and on the code behind
else if ((string)e.CommandName == "detail")
{
// Show the toast of information.
//ResetApprovalDetailFields();
PopulateApprovalDetailByWebUserId(webUserId,int.Parse((string)e.CommandArgument)
ApprovalDetailPanel.Visible = true;
}

Save data before leave the page in ASP.NET

I have asp.net page.
I don't have button "Save" on it (not my requirement).
I need to save page data before I leave this page.
I've tried to do it with events like Page_Unload but it doen't work...
Cause Save method uses data from elements on page like txtBox1.Text I can't use .ashx handler to make some ajax save (Or I don't know how to use page elements in handler as they are protected).
So is there any solution except serealizing form and pass it to handler?
You can use the onbeforeunload event.
It should fire before unload.
It allows you to ask back if the user really wants to leave. Check simillar example here.
<script language="JavaScript">
window.onbeforeunload = confirmExit;
function confirmExit()
{
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?";
}
</script>
In return you can save you required values.
Alternatively, you can send out an Ajax request when user leaves.
I know its late to answer , but its not late at all for people who is still working on same problem :
you just have to surround your execution function area with update panel like below
<asp:UpdatePanel runat="server" ID="blahblahId">
<ContentTemplate>
<asp:Button CssClass="hidden" runat="server" ID="btnAddInfo" OnClick="execBforeUnload"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAddInfo"/>
</Triggers>
</asp:UpdatePanel>
and your javascript code will be
var unloadEvent = function (e)
{
alert("hi");
__doPostBack("<%=btnAddInfo.ClientID%>", "");
};
window.addEventListener("beforeunload", unloadEvent);
Finally your C# method will be as follows:
public void execBforeUnload(object sender ,EventArgs e)
{
Session[lblNumber.Text] = null;
Application[lblNumber.Text] = null;
}

OnClick not working when i use both Onclick & OnClientClick

With this code, i try to Close a Window (the way i'm doing it works) but i have also an Onclick event which is ignored!
<script type="text/javascript">
function GetRadWindow() {
var oWindow = null;
if (window.radWindow) oWindow = window.radWindow;
else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
return oWindow;
}
function CloseDialog() {
GetRadWindow().close();
}
ASPX page:
<asp:Button ID="Button1" runat="server" Text="Soumettre ce ticket"
onclick="Button1_Click" OnClientClick="CloseDialog();return false;"/>
My application never enters Button1_click event, can anyone help me to find out why ?
thanks in advance
EDIT: HTML GENERATED FOR THE BUTTON
<input type="submit" id="Button1" onclick="CloseDialog();" value="Soumettre ce ticket" name="Button1"/>
This article kind of explains the problem. You need to return true in your JS if you want the server event to trigger. Otherwise, you have to return false.
And, it also looks like you will have to add the UseSubmitBehavior = false based on: OnclientClick and OnClick is not working at the same time?
This is especially evident after seeing that your generated HTML only has the CloseDialog() and not the call to Button1_Click. This change will concatenate to the end of your onclick.
<asp:Button ID="Button1" runat="server" Text="Soumettre ce ticket"
onclick="Button1_Click" OnClientClick="CloseDialog();"
UseSubmitBehavior="false"/>
I ran into this problem and using UseSubmitBehavior="false" nearly did the trick. Be sure to have your OnClientClick call set up correctly:
My code was using OnClientClick="return ValidateSearch();" which is incorrect. It should just be
<asp:Button ID="keywordSearch" runat="server" Text="Search" TabIndex="1"
OnClick="keywordSearch_Click" OnClientClick="if (!ValidateSearch()) { return false;};" />
See the Source here (and tell them thanks!)
If it is set up incorrectly, the OnClick function will not fire.
You are returning false in the onclientclick, so the event is returned before the postback. As a result, onclick never fires.
I just came across the same issue, and I think we're getting confused between server side enable="false" and client-side "disable".
The serverside property to enable a control using control.Enabled = "false"; is not the same as applying an Attribute, like control.Attribute.Add("disabled","disabled");
If you apply the Enabled = false; from the server side, you're actually turning off the control entirely, even if it's shown on the screen! Go on, try and right click the control and select Inspector or FireFly to see it. Nothing shows up. Nothing happens, because the control does not "exist".
Yet if you apply the Attribute property the control is visible to the server and the client, and you're able to inspect it.
What I did is set up the default environment on the ASP.net side by saying the control (asp:Button in my case), has Enabled="true" (or not saying anything, as that's the default anyway).
On the server side, upon PageLoad(), I make my button Visible (in my case I also had it defaulted to visible="false"), and add the appropriate Attribute values.
btnSEND.Visible = true;
btnSEND.Attributes.Add("disabled", "disabled");
That way the button is not enabled, but it's not entirely "invisible" to the client, and by using JavaScript in another area of my program, I control when to enable it or not.
This method also avoids having to use the UseSubmitBehavior="false".
in page_load, you pass the java script function using Add.Atribute method with return true. first it will load the javascript function. then it will load the button click event

asp.net - do AsyncPostBack using js

I've spent almost 3 hours for the following case:
I want to trigger an AsyncPostBack from JavaScript but to be able to send same parameter also because I need it on the server side.
I've tried a lot of situations but I am unable to find a clear example.
I have an update panel and when I am pressing a specific image button inside it, some popup is displayed. (the popup is hidden initially, and when the request is done, the display:none is removed=> it appears fine)
function BeginRequestHandler(sender, args) {
}
function EndRequestHandler(sender, args) {
document.getElementById('popup').style.display = '';
document.getElementById('overlay').style.display = '';
}
Well, the issue is that I need to make a js function to do the asyncPostBack because this js function will be called from a flash control when doing click on some portion of that control, so I want to simulate my click on the image. (because doing click this popup is displayed as you see above)
The issue is that I am not able to find anywhere such an example. This js function will receive a parameter so when I do the postback I want to be able to get that parameter on the server side. (this parameters seems to be the main problem =- how I send it to the server when I am doing postback?).
Sorry if I was not very clear, but can you give me some documentation for this or example?
Thanks a lot!
UPDATE:
Please note that the first case I've already done, using an ImageButton in my UpdatePanel:
<asp:ImageButton ID="lnkDetails" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Logo") %>'
AlternateText='<%# DataBinder.Eval(Container.DataItem, "Name") %>' OnCommand="lnkDetails_Command"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "TenantID") %>' Text="Click">
</asp:ImageButton></li>
What I only need is to "simulate" this click, using a javascript to make the async post back (because when I will call from flash, flash control will call my js method for postback)
To initiate an async postback from javascript:
__doPostBack(target,args)
target is the UniqueID of the UpdatePanel you want to target. Usually it works fine to just pass an empty string (will refresh all auto-updating panels).
The 2nd parameter is arguments that you can use for whatever you want.
Both will be available to you on the server:
Request.Form["__EVENTTARGET"]
Request.Form["__EVENTARGUMENT"]
This should work quite well:
<asp:ImageButton ID="ImageButton1" runat="server" OnClientClick='return doSomething(<%#Eval("SomeValue")%>);' />
EDIT: Try wrapping your JavaScript function with this:
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function () {
doSomething = function(argument){
__doPostBack("<%=ImageButton1.ClientID%>", argument);
return true; //don't know if you need this
}
});
EDIT: Check your ScriptManager and make sure that EnablePartialRendering is set to true.
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" ... />
In the code-behind:
protected override void RaisePostBackEvent(IPostBackEventHandler source, string eventArgument)
{
//call the RaisePostBack event
base.RaisePostBackEvent(source, eventArgument);
if (source == ImageButton1)
{
string arg = eventArgument;
}
}

Conditional confirm prompt in asp.net code behind

I have looked around for a way of implementing this. Here is a pseudocode representation of what I have:
bool hasData = ItemHasData(itemid);
Confirm = "false"; // hidden variable
if (hasData)
{
//Code to call confirm(message) returns "true" or "false"
if (Confirm == "true")
{
//Delete item
}
else if (Confirm == "false")
{
return;
}
}
The code to call confirm uses a asp:Literal control and sets it equal to the confirm. I can get the popup but only after the function exits. And it does nothing with the conditions after that.
The general consensus seems to be that calling the javascript at that specific line is impossible (makes sense due to the server side/client side gap), but how can I achieve this? I tried using the ConfirmButtonExtender from the ASP.NET AJAX Toolkit but I couldn't interact with the confirmbuttonextender object from the code behind when the object is set to runat="server".
edit:
Sorry, I did miss those tidbits. Thanks Icarus.
The control itself is the GridView (the pseudo version is actually from the gvData_RowCommand function)'s rowcommand. The first check looks to see if the CommandName is DeleteItem and if so goes into this.
The gvData's columns are set based off a list of headers (and the dataset) passed as the table it is working against is for multiple items with different required information. The gvData's data is there, I just need to get a Yes/No (or in reality it'll end up being Ok/Cancel) dialog to verify they want to delete the item when there is data.
One method I end up using in some situations is to have a Panel that displays the Confirm / Cancel buttons. This avoids the need to handle JavaScript events and uses ASP.NET entirely.
<asp:Panel ID="pDeleteConfirm" runat="server"
CssClass="AlertDialog"
Visible="False">
<p>Do you wish to delete the selected record?<br />
<asp:Button ID="btDeleteYes" runat="server" OnClick="btDelete_Click" Text="Delete" />
<asp:Button ID="btDeleteNo" runat="server" OnClick="btDelete_Click" Text="Cancel" />
</p>
</asp:Panel>
<asp:GridView ID="gvData" runat="server"
AutoGenerateColumns="False"
CssClass="GridView"
DataKeyNames="ID"
DataSourceID="sqlData"
EmptyDataText="There is no data entered in the system."
OnRowDeleting="gvData_RowDeleting">
......
</asp:GridView>
I use the OnRowDeleting event to show the Panel
protected void gvData_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// Show confirmation dialog
pDeleteConfirm.Visible = true;
// Select the row to delete
gvData.SelectedIndex = e.RowIndex;
// Cancel the delete so the user can use the confirm box
e.Cancel = true;
}
Handle the button Click events
protected void btDelete_Click(object sender, EventArgs e)
{
Button bt = (Button)sender;
switch (bt.ID)
{
case "btDeleteYes": // they confirmed a delete
sqlData.Delete();
break;
case "btDeleteNo": // they clicked cancel
// Do nothing
break;
default:
throw new Exception("Unknow button click in btDelete_Click");
}
// clear selection and hide the confirm box
gvData.SelectedIndex = -1;
pDeleteConfirm.Visible = false;
}
This isn't JavaScript but you can add in some UpdatePanels to do AJAX work on it.
Just one method to do it through ASP.NET rather than JavaScript handling.
The job of your code-behind is to render HTML out to the browser. It does this, and then the socket is closed and your server side code is no longer executing.
You'll have to implement this logic using a Javascript function on the client side.
Are you attempting to warn the user if there's data loaded before they take a certain action? Or perhaps before they try to leave the page? You'll have to popup the alert using script when that action happens on the page.

Categories

Resources