I currently have a listview on an ASP.NET webpage that displays "cottage" records from an Access database. The name of each cottage is displayed as a hyperlink so when clicked brings you to another webpage:
<li style="">Name:
<asp:Hyperlink ID="Cottage_NameLabel" NavigateURL="~/Cottage.aspx"
runat="server" Text='<%# Eval("Cottage_Name") %>' />
<br />
This works perfectly fine when selecting a hyperlink. What I want the system to do is to set the value of a publically declared variable (set in a module) to the Cottage_Name of the selected hyperlink. So say if i clicked on a hyperlink that said "cottage1", the public variable is set to "cottage1" and then the navigate URL opens the next webpage.
Would really appreciate it if anyone could help me do this!
Just use a LinkButton instead of a Hyperlink... Catch the click event and do whatever you want...
For instance:
<asp:LinkButton ID="Cottage_NameLabel" runat="server" Text="whatever" onclick="Cottage_NameLabel_Click" />
Then in CodeBehind:
protected void Cottage_NameLabel_Click(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)sender;
Session["MyCottageName"] = btn.Text;
Response.Redirect("Cottage.aspx");
}
In your Cottage.Aspx page you can check the value of the Session variable like this:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["MyCottageName"] != null)
{
string name = (String)Session["MyCottageName"];
...
}
You can pass the name as a querystring variable to the page. If you go this route, you need to make sure you URL encode the cottage name:
<a href='/Cottage.aspx?name=<%# Server.UrlEncode(DataBinder.Eval(Container.DataItem, "Cottage_Name")) %>'><%# Eval("Cottage_Name") %></a>
And then on cottage.aspx you can get the cottage name:
Dim cottageName As String = Request.QueryString("name")
This would be preferable to a button or other postback solution as it removes the need for a postback and then a redirect.
Related
i am trying to set the value of a textbox control in my aspx page as the value of a label text. I am using the following code but nothing happens. I have tried from the code behind file using c# and then I want to assign the textbox value to a session variable. If I just assign a string value like "Hello"it works, but otherwise nothing works.
protected void btnBook_Click(object sender, EventArgs e)
{
txtmtcid.Text = blbtest.Text;
Session["mtcid"] = txtmtcid.Text;
//Response.Redirect("booknow.aspx");
}
}
I also tried with Javascript, but no use:
var mtcid = parsedData.employeeCode;
document.getElementById('txtmtcid').value = mtcid;
The above js code works fine if I am assigning the value of mtcid to a label text, but not for the text box. please help.
You don't show things that could be messing this up.
and we can't see your markup used.
What does your page load event look like. Keep in mind that EVERY button click, post-back or ANY control on the page with a event code stub WILL RUN the page load event EVERY time. the page load event runs every time, and BEFORE your button click code runs.
So, if I have this markup:
<asp:Label ID="blbtest" runat="server" Text="zoo"></asp:Label>
<br />
<asp:TextBox ID="txtmtcid" runat="server"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button"
onclick="Button1_Click" />
<br />
And then code behind of this:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
txtmtcid.Text = blbtest.Text;
}
I then get this when I click on the button
So, we can see how the lable text is now copy to the text box.
You have to post more markup, and give futher detilas (such as some grid view, repeater or any other boatload of features that well explain your issue).
Note that you can double click on the button in the web forms designer to create a event click for the button. Its possible that no event code for the button exists, and your button click code and code behind never runs.
So this is the markup:
<asp:Label ID="blbtest" runat="server" ClientIDMode="Static">
</asp:Label>
<asp:Button ID="btnBook" runat="server" Text="Book Now"
CssClass="spaces-info__button" OnClick="btnBook_Click"/>
<asp:TextBox ID="txtmtcid" runat="server">
</asp:TextBox>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!(Session["username"] == null))
{
string usn = Session["username"].ToString();
lblusn.Text = usn;
}
}
protected void btnBook_Click(object sender, EventArgs e)
{
txtmtcid.Text = blbtest.Text;
Session["mtcid"] = txtmtcid.Text;
Response.Redirect("booknow.aspx");
}
updated js:
$(function () {
//dom is ready
var request = new XMLHttpRequest;
var usn = document.getElementById('lblusn').innerHTML;
//var usn = "juhaina.ahmed";
console.log(usn);
request.open('GET', "URL" + usn + ""); //of course I have replaced
the URL here
request.onload = function () {
var response = request.response;
var parsedData = JSON.parse(response);
console.log(parsedData);
var nm = parsedData.fullName;
document.getElementById('lblfullnm').innerHTML = nm;
var mtcid = parsedData.employeeCode;
document.getElementById('blbtest').innerHTML = mtcid;
document.getElementById('txtmtcid').value =
document.getElementById('blbtest').innerHTML
};
request.send();
});
I am new to js, and asp.net, so trying to browse whatever possible and work things out here. The session variable value is just not getting passed to next page. Honestly, i dont need the textbox, I dont know if label text can be stored in session variables. If thats possible, then all I want to do is assign the blbtest label text to the session variable, but that also didnt work,but if I am hard coding it like:
Session["mtcid"]="D-11234"
this works and the value of session variable is passed.
hence I am trying now with textbox. Please let me know if theres a better approach to this.
If there is a way to avoid the use of the label and textbox and simply pass the session variable, Session["username"] from the code behind to the javascript, that would be great. how can I do it?
I have hidden value:
<input type="hidden" name="orderRef" value="<%=orderRef%>" />
and I have a asp button, and i want to pass this param to aspx.cs function:
<asp:Button ID="submitForFree" runat="server" OnCommand="btnPromoBtn_Click" CommandArgument="<%=orderRef%>" class="primary-button" Text="PayNow"></asp:Button>
but in the function it show me a string "<%=orderRef%>"
How can i put it in CommandArgument?
You have two options
Set it in the code behind
Instead of trying to set it in the markup, set it in your codebehind, some time after OnInit.
this.submitForFree.CommandArgument = orderRef;
Read it from the request
Instead of passing the value in CommandArgument, just read it from the HttpRequest, where the hidden variable is exposed in Request.Form.
protected void btnPromoBtn_Click(EventArgs e, object sender)
{
//var arg = e.CommandArgument;
var arg = Request.Form["orderRef"];
DoSomethingWithCommandArgument(arg);
}
You can bind the CommandArgument like this:
CommandArgument='<%# orderRef %>'
But if your button is outside a Repeater, GridView, ListView etc you need to call DataBind manually every time the page is loaded.
protected void Page_Load(object sender, EventArgs e)
{
submitForFree.DataBind();
}
I have 2 aspx pages.. (view.aspx,edit.aspx).
under view.aspx I have grid which displays the following fields.
class,photo,photocaption,Edit which consists of class name, image of the class and a caption for the photo and a link button for edit which traverses to edit.aspx...
under edit.aspx, I have a table consisting of
Class(a drop down box),
Photo(text box with BROWSE button),
photocaption(Textbox).
when i click on the edit in view.aspx, I must get the entered values in the view.aspx grid by default in edit.aspx table..
Please help me to finish my Task...
Assuming your view.aspx page has a TextBox control for the class name called txtClassName, you can add a query string to your edit.aspx like this:
<asp:LinkButton ID="EditLink" runat="server" Text="Edit" PostBackUrl='<%# "edit.aspx?classname=" + txtClassName.Text %>' />
This creates the url in this format:
edit.aspx?classname=class1
Then, in the Page_Load event of your edit.aspx page, you can retrieve the selected class name like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string className = Request.QueryString["classname"];
// code to output the class details
// for example, if you want to add the class name to a dropdownlist:
DropDown1.Items.Add(className);
}
}
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.
I've a panel bar with some items in it. When I right click on the items and if I select "open in new tab", I need to open the link in a new tab.
For eg. if my page is "http:localhost/MyPage"
My grid is:
Name
abc
bcd
cde
When I click on the second item, the navigateUrl would be "http:localhost/MyPage/?Name=bcd"
This works fine.
But I want to hide the name in the url. Is there any other way, I can pass the name to next page without exposing it in the url. I could use sessions, but unfortunately, I cannot write it as a code for the default context menu.
You can use LinkButton objects. They will postback and then you can redirect requests to desired pages.
ASPX:
<asp:linkbutton id="lnkabcd" runat="server" text="abcd" onclick="lnkabcd_clicked"/>
C#:
public void linkabcd_clicked(object sender, EventArgs e)
{
Response.Redirect("URL OF TARGET PAGE");
}
Ofcourse it will be very cumbersome if you have lot of links. You can use grid (hope you are using it as you write in your question) and catch row event with command name and command argument properties.
To hide the url in addressbar of the browser, you need to do URL rewriting. For more on URL rewriting please visit these pages on codeproject and msdn.
You could set a cookie - that way the next time the user returns you could even return them to the same page (if you wanted to).
You may find this article helpful when deciding if this is a good option for you.
If you need to set the cookie on the client-side, then this article should help you out.
As #TheVillageIdiot said, url rewriting is a better approach. But you can use cross-page posting ability as well. Check it out:
Markup
<asp:HiddenField ID="HiddenField1" runat="server" ClientIDMode="Static" />
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl="~/Second.aspx" Text='<%# Bind("Text") %>' OnClientClick='<%# "LinkButton1_Click(\"" + Eval("Value") + "\")" %>' />
</ItemTemplate>
</asp:Repeater>
<script type="text/javascript">
function LinkButton1_Click(v) {
document.getElementById('HiddenField1').value = v;
}
</script>
As you can see in the preceding code snippet, you have to add a hidden field to store the selected item by a simple javascript. Also I defined a property, called SelectedValue to get the value of the hidden field on the otherside.
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
Repeater1.DataSource = new[] {
new { Text = "Item 1", Value = "Item 1" },
new { Text = "Item 2", Value = "Item 2" },
new { Text = "Item 3", Value = "Item 3" }
};
Repeater1.DataBind();
}
public string SelectedValue
{
get { return HiddenField1.Value; }
}
Second Page
Add following directive to the destination page.
<%# PreviousPageType VirtualPath="~/Default.aspx" %>
Finally, you have access to the previous page via PreviousPage property of Page class.
string value = ((_Default)this.PreviousPage).SelectedValue;