I have an asp.net linkButton (or imageButton) control in my profile.aspx page. I'am checking Request.Querystring("id") in the page below in the code behind.
http: //localhost:42932/profile.aspx?id=1
When I first load the profile page it is not posted back. It is ok!. When I go to another users profile (the same page just the query string is different) using imageButton control with the adddress;
http: //localhost:42932/profile.aspx?id=2
it is posted back. I dont want it to be posted back. But if I go to this page with a regular html input element like
a href = "http: //localhost:42932/profile.aspx?id=2"
it is not posted back. So I want the image button behave like an html input element.
Here is my imageButton;
ASPX:
<asp:ImageButton ID="imgProfile" ImageUrl="images/site/profile1.png" runat="server"/>
.CS
imgProfile.PostBackUrl = "profile.aspx?id=" + Session["userID"];
Edit:
if (!IsPostBack)
{
Session["order"] = 0;
}
This control is in the page load. So it should be !postback with state I mentioned above. Because all the other functions are working when Session["order"] = 0
Make use of OnCLientClick instead of OnClick, so that you only run client side code. Then, sepcify that you return false;
i.e.
<asp:ImageButton ID="imgProfile" ImageUrl="images/site/profile1.png" runat="server" OnClientClick="return false;" />
But, why use a server control, when this can be done with a normal <img .. html control?
Rather than specifying a PostBackUrl I would recommend using Response.Redirect() in the button click event handler:
public void imgProfile_Click(object sender, eventArgs e){
Response.Redirect("profile.aspx?id=" + Session["userID"]);
}
Or alternatively, just use a Hyperlink control and set the NavigateUrl property during Page_Load:
<asp:HyperLink ID="imgProfile" runat="server"><img src="images/site/profile1.png" /></asp:Hyperlink>
imgProfile.NavigateUrl = "profile.aspx?id=" + Session["userID"];
Related
I'm unable to retrieve the content of a textbox that's inside an accordion panel. My markup is as follows:
<juice:Accordion ID="Accordion1" runat="server">
<juice:AccordionPanel ID="AccordionPanel1" runat="server" Title="Media ID">
<PanelContent>
<asp:Label ID="LabelMediaID" runat="server" Text="Media ID" AssociatedControlID="TextBoxMediaID"></asp:Label>
<asp:TextBox ID="TextBoxMediaID" runat="server"></asp:TextBox>
</PanelContent>
</juice:AccordionPanel>
My server side code is triggered when the user clicks a button:
protected void ButtonSearch_Click(object sender, EventArgs e)
{
// Retrieve controls within accordion panels
TextBox TextBoxMediaID = (TextBox)AccordionPanel1.FindControl("TextBoxMediaID");
string mediaID= "abc";
if (TextBoxMediaID != null)
mediaID= TextBoxMediaID.Text;
I'm able to successfully retrieve my textbox control but when I try to access its Text property it's always empty.
Can someone help me? I'm afraid I'm reasonably new to the world of ASP.NET, Juice etc. Thanks.
It turned out that my problem was caused by having an ASP.NET project that used master pages.
If you're not using master pages then
TextBox TextBox1 = (TextBox)AccordionPanel1.FindControl("TextBox1");
string content= Request.Form[TextBox1.UniqueId];
works perfectly. However, when using a master page with a ContentPlaceHolder then use the following code:
ContentPlaceHolder cph = (ContentPlaceHolder)Master.FindControl("MainContent");
TextBox TextBox1 = (TextBox)AccordionPanel1.FindControl("TextBox1");
string content= Request.Form[cph.UniqueID + "$" + TextBox1.UniqueId];
Hopefully this will save someone a lot of time!
Option 1 - Quick fix
For your page, make ClientIDMode="static" in page directive and then use Request.Form["TextBoxMediaID"] to get value of your text box.
Option 2 - Debug
Go to debug mode and try AccordionPanel1.Controls[0].("TextBoxMediaID"); or AccordionPanel1.Controls[0].Controls[0].("TextBoxMediaID"); because you don't know how many server side controls are there. Simple solution would be to use find control recursive function.
Clicking on ASP.NET button redirects to correct website but on the same tab, not in a new tab what i need to do. What's wrong with the code OnClientClick="aspnetForm.target ='_blank';" below? Why it is not enough alone and what else need to be done?
The following ASP.NET code for the button control is:
<asp:Button ID="btnGenerateReport" runat="server" Text="Generate Report"
OnClick="btnGenerate_Click" OnClientClick="aspnetForm.target ='_blank';" />
I know two methods for redirecting the page to new tab in asp
1) The first method which you are already using and it works also. Make an onclientclick event of Button and on code behind of Button
Click write the following code:-
button.OnClientClick = "aspnetForm.target='_blank'"; Response.Redirect("yourpage.aspx");
2)You can also use javascript
button.Attributes.Add("onclick", "window.open('yourpage.aspx');return false;");
Both the method will redirect your page to new tab on clicking the button.
The error with your code is OnClientClick = "aspnetForm.target='_blank;'" remove the semicolon after '_blank' and it will work
If you are looking out for server side code to open a new window on Button Click, then here's how to do so.
Add the following script to the section of your page
<script language="javascript" type="text/javascript">
function openNewWin(url) {
var x = window.open(url, 'mynewwin', 'width=600,height=600,toolbar=1');
x.focus();
}
</script>
Then add a Button Control in the following manner
<asp:Button ID="btnOpenPop" runat="server" Text="Open Pop"
onclick="btnOpenPop_Click" />
Finally add some code in the code behind file
protected void btnOpenPop_Click(object sender, EventArgs e)
{
string url = "http://www.dotnetcurry.com";
ClientScript.RegisterStartupScript(this.GetType(), "OpenWin", "<script>openNewWin('" + url + "')</script>");
}
You can't use target attribute on button.
You have to use javascript function window.open()
Check this:
Window open() Method
I don't post links lightly, but I found this site explains all and it has solved my problem too.
http://dotnetspidor.blogspot.co.uk/2009/01/open-new-window-in-aspnet-web-page_28.html
I have this hyperlink called “SEND” in a ASP page called Home and here it is:
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("Post_ID", "~/RCA.aspx?Post_ID={0}") %>'
Text="SEND"></asp:HyperLink>
</ItemTemplate>
when the user clicks the hyperlink it goes to another page called RCA and in this page there is a Button and here it is the code:
<asp:Button ID="btnRCA" runat="server" onclick="Button1_Click"
Text="Assign RCA" Width="147px" />
so I want this button to be visible only when clicked the hyperlink in the HOME page. I am planning to have another button or control in the RCA page that will make it invisible when clicked or before someone leaves the page they have to make it invisible the Button by clicking some other control. can someone help me with this? thanks
Use a QueryString parameter.
Home.aspx
//When linked to RCA.aspx from Home.aspx, a parameter called ShowButton=1 is included
//in the URL.
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl='<%# Eval("Post_ID", "~/RCA.aspx?Post_ID={0}&ShowButton=1") %>'
Text="SEND"></asp:HyperLink>
RCA.aspx
//By default, since you want the button to NOT appear for all incoming traffic EXCEPT
//that which came from Home.aspx, the button's Visible property is set to false.
<asp:Button ID="btnRCA" runat="server" onclick="Button1_Click"
Text="Assign RCA" Width="147px" Visible="false" />
RCA.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
//If the querystring has a parameter called ShowButton and it's equal to "1",
//then set the button to Visible = true.
//Else, do nothing, keeping the button in it's default, Visible=false state.
//By putting this in an "IsPostback == false" check, you can guarantee that this will
//only happen on first page_load, and won't be triggered again even if you do other
//actions in the page that cause Postback
//For example, if you don't use this !IsPostback check, and you end up creating some
//new function that causes the button to be hidden again, but then you make a
//selection from a dropdown list that causes postback, you will trigger the call to
//make the button Visible again, even though that's probably what you don't want at
//this point, since your other new function set it to Visible = false.
if (!IsPostback)
{
if (Request.QueryString["ShowButton"] == "1")
{
RCAbtn.Visible = true;
}
if (Request.QueryString["Post_ID"] != null)
{
//do whatever you need to with the post ID
}
}
}
SomeOtherPage.aspx.cs
Response.Redirect("RCA.aspx?Post_ID=1234"); //button will be invisible
And then let's say later that you want to re-direct from some other page and have the button be visible, like the redirect from Home:
Response.Redirect("RCA.aspx?Post_ID=1234&ShowButton=1"); //button will be visible
If you don't like cluttering up your URL or you feel that it looks tacky to have what you are doing so plainly available to the user's eyes, you don't necessarily need to use "ShowButton". You could say ?Post_ID=1234&fkai3jfkjhsadf=1, and then check your query string for "fkai3jfkjhsadf". I like to do that sometimes because then from the users point of view, it makes me look like I'm doing something really technical and encrypted, and not just passing around a bunch of basic instructions in plain English :) Downside there is you need keep track of your own query string parameters.
Edit:
If you want to get the URL with only the Post_ID and nothing else, you can do this:
string currenturl = Request.Url.ToString(); //get the current URL
string urlToSend = currenturl.Substring(0, currenturl.IndexOf("?")); //cut off the QueryString entirely
urlToSend += "?Post_ID=" + Request.QueryString["Post_ID"]; //re-append the Post_ID
Be aware that your call to Substring will cause an exception if the URL doesn't have a QueryString, so please patch that up in whatever way works best for you (try/catch, etc.).
After that, you should just be able to use the "urlToSend" string in your mailMessage.Body.
on your second page in your page_load, try this:
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Post_ID"] != null)
{
btnRca.Visible = true;
}
}
I don't know how you want to handle the visibility of this button in other cases, but this should answer your particular question.
<td><input type="image" style="border-width:0" alt="Do an Edit" title="Edit the repeater row" tabindex="0" src='<%=ResolveUrl("~/css/image.png") %>' onclick='showModal(<%#Container.ItemIndex %>); return false;'/></td>
I am already using onClick() for invoking a javascript method. Now, I want to add another an event to this element to trigger a server side event- say using onKeyUp- and pass 1 parameter on the repeater to the server side.
Is this possible or I have to first call javaScript and then Javascript on the page will callinto my server side. (I think this way the method in the server side code has to be static).
You can add an invisible LinkButton to your page (for example LinkButton with a blank text) and from your client-side onKeyUp event call LinkButton's click() method and catch Click event server side.
To pass parameter you can use any control that is readable both client-side and server side, for example hidden field.
For example - your HTML markup can look something like this:
<asp:TextBox ID="xtxtMyText" runat="server" onkeyup="doMyPostback()"></asp:TextBox>
<asp:HiddenField ID="xhidParam" runat="server" />
<asp:LinkButton ID="xlnkMyPostBack" runat="server" OnClick="xlnkMyPostBack_Click"></asp:LinkButton>
<script type="text/javascript">
function doMyPostback(e) {
var evt = window.event ? window.event : e;
if (evt.keyCode == 13) {
document.getElementById('xhidParam').value = document.getElementById('xtxtMyText').value;
document.getElementById('xlnkMyPostBack').click()
}
}
</script>
Here you have a textbox that triggers keyUp event, hidden linkbutton that causes server-side Click event and hidden field that is used to pass parameter. In this scenario if user hits Enter, content of textbox is copied to the hidden variable and the linkbutton is clicked.
On the server side you can handle that event and read the passed parameter from the hidden field, e.g.
protected void xlnkMyPostBack_Click(object sender, System.EventArgs e)
{
Response.Write(xhidParam.Value);
}
How do I execute Javascript after submission of an ASP.NET form?
For example, if I got a submit button click after submission of page then I want to display a DIV which I have hidden on page.
Since you are using a full postback, you can easily include the javascript from the code-behind.
Place your JavaScript Code inside a Placeholder
<asp:Placeholder ID="javascriptPlaceholder" runat="server" Visible="false">
<%-- Your Javascript here -->
</asp:Placeholder>
I assume in your OnClick handling method, you are getting the value of the form and do something else with it. In this method you could set the Visibile property of the placeholder:
this.javascriptPlaceholder.Visible = true;
If you would only want to convert the hidden div to a visible one then that can be done on the server side. In the aspx, give the div an id and a runat="server". Then in the server side, set its display style attribute to 'block'. For example, say the div is defined as
<div id="divTest" runat="server" style="display:none;">
This is a div test
</div>
Then in the post event of the submit button you can set its style as given below:
divTest.Style["display"] = "block";
If you only want to emit scripts after the post then using the ScriptManager you could use:
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "ScriptRegisterTest", "alert('Testing');", true);
Maybe I'm not understanding your question, but if you're going back to the server and doing some processing, why do you need javascript? Just add a runat="server" to the div in question and make it visible after the processing.
<div id="YourDiv" runat="server" Visible="false" >
...Whatever is here
</div>
Code Behind:
protected void Button_Click(object sender, EventArgs e)
{
/*
Your Logic Here
*/
YourDiv.Visible = true;
}