I have an imagebutton in ASCX file which code is
<asp:ImageButton ID="ImageButtonEdit" runat="server" CommandArgument='<%#Eval("ItemID") %>' ImageUrl="~/images/edit.gif" OnClick="ImageButtonEdit_Click" />`
and the behind code is
protected void ImageButtonEdit_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
ImageButton editing = (ImageButton)sender;
Response.Redirect(Globals.NavigateURL(PortalSettings.ActiveTab.TabID, "AddCollectionItem", "mid=" + this.ModuleId) + "?ID=" + Convert.ToInt32(editing.CommandArgument));
}
Problem is it doesn't Redirect to the page or any other thing?
The Response.Redirect when I try to use it in general it doesnt work in this part only and in the other site it is working well
You're trying to pass a URL with two ?s in it, that might be causing part of the problem. Your second argument, ?ID= should be "&ID="
protected void ImageButtonEdit_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
ImageButton editing = (ImageButton)sender;
Response.Redirect(Globals.NavigateURL(TabID, "AddCollectionItem", "mid=" + this.ModuleId + "&ID=" + Convert.ToInt32(editing.CommandArgument)));
}
You also could pass that inside of NavigateURL instead of outside of NavigateURL as you are doing.
If your module is properly inheriting from PortalModuleBase you don't need PortalSettings.ActiveTab.TabID, just call TabID directly.
Also, are you doing any of this inside of an Update Panel? Perhaps that is causing redirect issues?
Related
Button on click method is not calling
Button code :
<asp:Button ID="personalSub" runat="server" ValidationGroup="personal" Text="Save" CausesValidation="false" OnClick="InsertPersonalDetail" />
C# Code :
protected void InsertPersonalDetail(object sender, EventArgs e)
{
Console.WriteLine("hello");
MessageBox.Show("hello");
}
If you have any problem on the page then you must see a compiler error.
You do NOT have compiler error witch is means that asp.net finds the InsertPersonalDetail function on code behind.
From what I see you call inside the button click two functions that are for desktop programming (not for web page).
Neither one can have any visible effect on your click - there is no console there to see anything, neither user interface to open the MessageBox.
protected void InsertPersonalDetail(object sender, EventArgs e)
{
Console.WriteLine("hello"); // have no effect on web page
MessageBox.Show("hello"); // have no effect on web page
}
So its called but you don't see it by just wait a pop up to appears
To check this out, run it with debuger and add a break point there.
Or add a literal on page and add some text there to verify that is called.
eg, add on page
<asp:Literal runat="server" ID="txtLiteral" />
and on code behind
protected void InsertPersonalDetail(object sender, EventArgs e)
{
txtLiteral.Text += "InsertPersonalDetail called <br />";
}
I have an asp.net application, where the user would click a button and launch another page (within the same application). The issue I am facing is that the original page and the newly launched page should both be launched.
I tried response.redirect, but that tends to unload the original page.
Any suggestions?
This button post to the current page while at the same time opens OtherPage.aspx in a new browser window. I think this is what you mean with ...the original page and the newly launched page should both be launched.
<asp:Button ID="myBtn" runat="server" Text="Click me"
onclick="myBtn_Click" OnClientClick="window.open('OtherPage.aspx', 'OtherPage');" />
Edited and fixed (thanks to Shredder)
If you mean you want to open a new tab, try the below:
protected void Page_Load(object sender, EventArgs e)
{
this.Form.Target = "_blank";
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Otherpage.aspx");
}
This will keep the original page to stay open and cause the redirects on the current page to affect the new tab only.
-J
If you'd like to use Code Behind, may I suggest the following solution for an asp:button -
ASPX Page
<asp:Button ID="btnRecover" runat="server" Text="Recover" OnClick="btnRecover_Click" />
Code Behind
protected void btnRecover_Click(object sender, EventArgs e)
{
var recoveryId = Guid.Parse(lbRecovery.SelectedValue);
var url = string.Format("{0}?RecoveryId={1}", #"../Recovery.aspx", vehicleId);
// Response.Redirect(url); // Old way
Response.Write("<script> window.open( '" + url + "','_blank' ); </script>");
Response.End();
}
Use an html button and javascript? in javascript, window.location is used to change the url location of the current window, while window.open will open a new one
<input type="button" onclick="window.open('newPage.aspx', 'newPage');" />
Edit: Ah, just found this: If the ID of your form tag is form1, set this attribute in your asp button
OnClientClick="form1.target ='_blank';"
You should use:
protected void btn1_Click(object sender, EventArgs e)
{
Response.Redirect("otherpage.aspx");
}
I am familiar with creating and persisting dynamic controls on the first load of a page and on subsequent postbacks but I am having trouble with the following user initiated scenario...
In my demo I have a placeholder, two buttons and a literal
<div>
<asp:PlaceHolder ID="phResponses" runat="server" />
</div>
<div>
<asp:Button ID="btnAdd" Text="Add" runat="server" OnClick="Add"/>
<asp:Button ID="btnInspect" Text="Inspect" runat="server" OnClick="Inspect"/>
</div>
<div>
<asp:Literal ID="litResult" runat="server"/>
</div>
I want the user to be able to click the add button to provide a response so I have...
protected void Page_Init(object sender, EventArgs e)
{
BuildControls();
}
protected void Add(object sender, EventArgs e)
{
BuildControls();
}
protected void BuildControls()
{
phResponses.Controls.Add(new LiteralControl { ID = "response_" + _Count.ToString() });
_Count++;
}
_Count is a static member variable to enable me to have unique ids for the new controls. I realise I need to rebuild the dynamic controls on Page_Init but the problem is that I end up with 2 new Literal controls on every postback. Also if any Text property is put into the new controls it is lost when the controls are rebuilt.
So how do I avoid adding multiple controls and how do I persist newly added properties when rebuilding these controls?
I use the following to inspect the responses
protected void Inspect(object sender, EventArgs e)
{
foreach (var control in phResponses.Controls)
{
if (control is LiteralControl)
{
litResults.Text += "<p>" + control.Text + " : " + control.ID + "</p>";
}
}
}
Which itself adds another unwanted control because of the rebuilding on Page_Init
I'd not sure that I quite understand what you're asking, but it looks like you just want to ensure that BuildControls is only called once per lifecycle. You could do that by making the following changes:
Add a new private bool _isControlsBuilt = false;.
Change Page_Init to check _isControlsBuilt before calling BuildControls.
Set _isControlsBuilt to true within BuildControls.
Make sure that BuildControls occurs earlier in the page lifecycle than Page_Init.
As for losing the values of controls on postback, it'll be that they're never hitting the viewstate. I'm not sure if it'd work, but my first guess would be to add a line to the end of BuildControls to call Page.RegisterRequiresControlState:
protected void BuildControls()
{
LiteralControl newLiteral = new LiteralControl { ID = "response_" + _Count };
this.RegisterRequiresControlState(newLiteral);
phResponses.Controls.Add(newLiteral);
_Count++;
_isControlsBuilt = true;
}
If that doesn't work (which might imply that it's the _view_state, not the _control_state that matters to you here), you may need to look at rolling your own viewstate. I wrote about how to do that in my answer to #3854193, which you might find useful.
I have the following ASP.NET code:
<div id="panelIssue" runat="server" style="width: 450px; height: 320px;">
<gsl:IssueUC ID="ucIssue" runat="server"
OnItemSaved="ucIssue_ItemSaved"
OnItemCancelled="ucIssue_ItemCancelled" />
</div>
and then have an asp:Button on the page that simply call some methods and set some properties of the custom user control like
protected void btnNewIssue_Click(object sender, EventArgs e) {
ucIssue.ChangePanelMode(PanelModeEnum.Add);
ucIssue.FirmID = Convert.ToInt32(Page.Session["FirmID"]);
ucIssue.loadObject();
}
I know that I can use the div to show a jquery modal dialog but the question is how to set the usercontrol properties and call their methods?
I can evaluate also a different approach (e.g. changing the usercontrol).
Thanks in advance
Lorenzo
What I can suggest is that in the click event, after setting the control properties, you can emit a piece of javascript that will create and display a modal when the page finishes loading upon reaching the client.
Try something along the lines of the following snippet (not tested, may need small tweaking to work, but I hope you get the big idea):
protected void btnNewIssue_Click(object sender, EventArgs e) {
ucIssue.ChangePanelMode(PanelModeEnum.Add);
ucIssue.FirmID = Convert.ToInt32(Page.Session["FirmID"]);
ucIssue.loadObject();
//emit client script that will create and show the modal dialog
ClientScriptManager clientScriptManager = Page.ClientScript;
string scriptText = "$(document).ready(\n" +
"function() {\n" +
"$('#panelIssue').dialog({\n" +
"autoOpen: false,\n" +
"modal: true,\n" +
"width: 450,\n" +
"height: 320,\n" +
"title: \"Some title\",\n" +
"resizable: false,\n" +
"draggable: false });\n" +
"$('#panelIssue').dialog('open');\n" +
"});";
clientScriptManager.RegisterClientScriptBlock(this.GetType(), "MyScript", scriptText, true);
}
You should also look at the capabilities of the dialog creation in jQuery to see what other settings will fit your scenario better. I just used a few of the params that I have in an example of mine.
We are running following javascript function:
function btn_AddToList_Click() {
var filePath = document.getElementById("FileUpload").value;
if(filePath.length > 0)
{
var opt = new Option(filePath,filePath);
var listBox = document.getElementById("ListBox");
listBox.options[listBox.options.length] = opt;
}
}
Function binding:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btn_AddToList.Attributes.Add("onclick", "btn_AddToList_Click(); return false;");
}
}
HTML:
asp:FileUpload ID="FileUpload" runat="server" Width="394px"
asp:ListBox ID="ListBox" runat="server" Width="394px"
asp:Button ID="btn_AddToList" runat="server" Enabled="true" Text="Add"
Issue is that value of "FileUpload" is not get cleared after we click "Add" button. Any help?
You can not set/clear the value of FileUpload control programmatically. That is a restriction for a security reason. Consider this if this restriction was not there, you could set the value of FileUpload control to some arbitrary file and upload it to your server. You won't be able to achieve this in current shape.
As a work around you can try to bring another textbox exactly on top of textbox part of FileUpload control. This way you will be give the same feeling what you are trying to achieve. But that is also not ideal and may not work properly.