I have a webpage: Menu.aspx, where I have the following controls of relevance:
a DropDownList with some values
a hyperlink, which redirects me to a page called Edit.aspx.
In my Edit.aspx page, I have a cancel button, which redirects the user back to the Menu page
What I would like to do is when my user clicks on the hyperlink to go to the Edit page,index of the DropDownList is preserved in a query string In my Menu.aspx page I have the following aspx code, but I am not sure how to proceed
<asp:HyperLink
ID="lnkEdit"
NavigateUrl='<%# "Edit.aspx?" + Eval("UserID") + ...not sure... %>'
</asp:HyperLink>
<asp:DropDownList
ID="myDropDown"
...some <asp:ListItems/>
</asp:DropDownList>
EDIT: Clarified why Im using NavigateURL. Because my query string already does an Eval to determine the user ID.
I would use a LinkButton control with a server-side OnClick event.
<asp:LinkButton ID="lbtn1" runat="server" OnClick="lbtn1_Click"
CommandArgument='<%#Eval("UserID") %>' />
Server side method:
public void lbtn1_Click(object sender, EventArgs e)
{
LinkButton lbtn = (LinkButton)sender;
string userID = lbtn.CommandArgument;
string dropDownValue = myDropDown.SelectedValue;
string navigateUrl = string.Format("Edit.aspx?userid={0}&dropdown={1}",
userID, dropDownValue);
Response.Redirect(navigateUrl);
}
EDIT: As Royi Namir points out below, javascript is a better option if you can use that. This creates an unnecessary round trip to the server.
Try something like this
<asp:HyperLink ID="lnkEdit"
NavigateUrl='<%# "Edit.aspx?" + Eval("UserID") +
"&menuid=" + myDropDown.SelectedValue %>'> MyText </asp:HyperLink>
and also set AutoPostBack to true on your drop down. Whenever you will change your dropdown, new selected value bind with hyperlink navigate url.
Here's an easy way is to use Session
Example:
Session["SessionName"] = idDropDownList;
On another page, access only the content of the session
string idDropDownList = (string)Session["SessionName"];
I hope I helped.
Related
I am using a Repeater to get items from the database.
I place every item into the web user control page, with this code:
<%#DataBinder.Eval(Container.DataItem, "XXXX")%>
XXXX = attribute from the database, for example: Username.
In the .cs file of the web user control page, I want to get the ID(which is also in the database) from every single repeated element, any clue how I can do that?
For example:
The <%#DataBinder.Eval(Container.DataItem, "Username")%> is showing the username of every person who is in the database. And in the .cs-file I want to get the ID of that username. So I can use a SQL-query like this when a button is clicked next to that repeated username:
UPDATE Table SET Username = "Mike" WHERE ID = '" + #### + "'
I don't know what to write in place of the ####.
This: #### = ID from the repeated Username where the button is clicked.
Thanks in advance!
With a Repeater, a good way is to use a command button.
In you .aspx, define a command button in the repeater ItemTemplate:
<asp:Repeater ID="repUsers" runat="server" OnItemCommand="repUsers_ItemCommand">
<ItemTemplate>
<asp:Button runat="server" CommandName="select" CommandArgument='<%#Eval("Id")%>' Text="Select" />
<span><%#Eval("Username")%></span>
<br />
</ItemTemplate>
</asp:Repeater>
In your .cs, capture the command button clicks:
protected void repUsers_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "select")
{
int userId = int.Parse(e.CommandArgument.ToString());
// do your db update here...
}
}
In your aspx page do the following:
<asp:Repeater OnItemCommand="ButtonCommandEvent" ID="myRepeater" runat="server">
<ItemTemplate>
...
<asp:HiddenField ID="recordId" runat="server" value='<%# Eval("id") %>' />
...
</ItemTemplate>
Then in your code behind you can wire your button to do the following:
void ButtonCommandEvent(Object obj, RepeaterCommandEventArgs e)
{
var temp = e.Item.FindControl("recordId") as HiddenField;
//use the value in temp to insert into your database
//UPDATE Table SET Username = "Mike" WHERE ID = temp
}
This will work as long as you have 1 button next to each repeated item.
How do I submit the id from one page to another page in ASP.NET using C#? I have a GridView control and each record contains a link button:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtn_naatscat" runat="server" Text="arshad" CommandName="view_user_naats_gv" Font-Underline="false" />
</ItemTemplate>
</asp:TemplateField>
I've bound the id to link button:
if (e.Row.RowType == DataControlRowType.DataRow)
{
((LinkButton)e.Row.FindControl("lbtn_naatscat")).CommandArgument
= DataBinder.Eval(e.Row.DataItem, "cate_id").ToString();
((LinkButton)e.Row.FindControl("lbtn_naatscat")).Text
= DataBinder.Eval(e.Row.DataItem, "title").ToString();
}
now I want to pass this id to another page when user click to this link button.
You can handle the LinkButton's Command event and use the CommandArgument:
void LinkButton_Command(Object sender, CommandEventArgs e)
{
if(e.CommandName == "view_user_naats_gv")
{
Resonse.Redirect("UserNaats.aspx?catID=" + e.CommandArgument.ToString());
}
}
You can create a Session for this purpose.
Do as follows:
Session["Id"]=e.CommandArgument.ToString() //Id you want to pass to next page
In this way your Session variable will get created. And you will be able to access it on the next page.
While retrieving it on next page:
Id=Session["Id"]
Other Alternatives:
View state
Control state
Hidden fields
Cookies
Query strings
Application state
Session state
Profile Properties
State management Techniques in ASP.NET:
http://msdn.microsoft.com/en-us/library/75x4ha6s%28v=vs.100%29.aspx
Hope its helpful.
Store it in a hidden field, and access it on the next page in the code behind file.
Here is another SO post with
Another example of passing hidden field in ASP
Best of luck!
I would use a HyperLinkField field like so
<asp:HyperLinkField DataNavigateUrlFields="Database ID Field"
DataNavigateUrlFormatString="~/PageName.aspx?ID={0}"
DataTextField="Database ID Field" HeaderText="ID" />
And then on the page that you're redirecting to, you would check the query string for ID
string id = Request.QueryString["ID"]
Set the PostBackUrl and CommandArgument properties of the LinkButton in your source page like so:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtn_naatscat" runat="server" Text="arshad" CommandName="view_user_naats_gv" Font-Underline="false" PostBackUrl="~/YourPage.aspx" CommandArgument='<%# Eval("submitid")%>' />
</ItemTemplate>
</asp:TemplateField>
Then on your page that is being submitted to you can place the following directive to the DestinationPage.aspx page:
<%# PreviousPageType VirtualPath="~/SourcePage.aspx" %>
I don't know what type of container the <ItemTemplate> tag is from (ListView?). However you can find the control in the DestinationPage.aspx page like so:
if (Page.PreviousPage != null && Page.PreviousPage.IsCrossPagePostback) {
// if you use the PreviousPageDirective you should be able to access the ListView
// property directly
// var listView = Page.PreviousPage.MyListView;
var listView = Page.PreviousPage.FindControl("MyListView") as ListView;
var linkButton = listView.FindControl("lbtn_naatscat") as LinkButton;
var submitId = linkButton.CommandArgument;
}
Using Row_Command event you can send it next page using query string.
protected void gvDeals_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName.Equals("MyCommand")
{
string value=calculate();
Response.Redirect("~/MyPage.aspx?item=" + value);
}
}
I think it would make more sense to use a hyperlink in your GridView.
For example:
<ItemTemplate>
<asp:HyperLink ID="link_naatscat" runat="Server"
Text="arshad"
ToolTip="arshad"
NavigateUrl=<%#"~/YourPage.aspx?id=" + Eval("submitid")%>>
</asp:HyperLink>
</ItemTemplate>
Send the id this way
Response.Redirect("/yourpage.aspx?" + id);
And get it this way
String s = "";
foreach (String item in Request.QueryString.Keys)
{
s = Request.QueryString[item];
}
if (s != "")
{
id = Int32.Parse(s);
}
I have few controls on asp.net web form page and i want to link a code behind function to Hyperlink to resolve URL
HTML & Code Behind Example
<asp:HyperLink ID="hyplnkVideo" runat="server" NavigateUrl='<%# this.getVideoPageURL()%>'>
<div id="dAlbumCategory" class="AlbumCategoryIcon">
<asp:Image ID="Image1" ImageUrl='~/Images/gallery/Videos.png' runat="server" />
</div>
</asp:HyperLink>
protected String getVideoPageURL()
{
string url;
int PageID = Helper.GetPageIDbyName("Videos.aspx", Request["Language"]);
url = "~/en/Videos.aspx?PageID=" + PageID + "&Language=" + Request["Language"];
return url;
}
This Hyperlink control is not inside any grid view or repeater control. I tried several way but for some reason it doesn't call the function.
I would appreciate help in this regard
try in this way...
<% getVideoPageURL(); %>
<asp:HyperLink ID="hyplnkVideo" runat="server">
<div id="dAlbumCategory" class="AlbumCategoryIcon">
<asp:Image ID="Image1" ImageUrl='~/Images/gallery/Videos.png' runat="server" />
</div>
protected void getVideoPageURL()
{
string url;
int PageID = Helper.GetPageIDbyName("Videos.aspx", Request["Language"]);
url = "~/en/Videos.aspx?PageID=" + PageID + "&Language=" + Request["Language"];
hyplnkVideo.Attributes.Add("href", url);
}
This is running code.. so u can try it
Simply try this:
NavigateUrl="<%= getVideoPageURL() %>"
The appropriate tag would be <%=, not <%#, as the <%# and %> tags indicate that you want to run data binding code (as in, something you've returned from the database).
It has been a while since this question was asked and marked as resolved.
However for the sake of providing a more appropriate resolution please call DataBind on your page code-behind in page load or render.This will inject the necessary href string.
I created LinkButton that located inside of Repeater Control.
CategoryID is a variable in LinkButton Control that have to get value after Repeater Control was bound to data. But CategoryID always get zero.
I have the following ASP and C# code:
<asp:Repeater ID="rpt1" runat="server"
OnItemDataBound="rpt1_ItemDataBound"
OnItemCommand="rpt1_ItemCommand">
<ItemTemplate>
<div>
<%# Eval("Name") %>-<%# Eval("CollectionType")%>
<asp:LinkButton ID="LinkButton1" runat="server" Text="[edit item]"
PostBackUrl='AddItem.aspx?CategoryID=<%# Eval("CollectionID")%>' />
</div>
</ItemTemplate>
</asp:Repeater>
Code behind:
public void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<GlassesCollection> gc = BL.GetDataBL.GetCollection();
rpt1.DataSource = gc;
rpt1.DataBind();
}
}
Any idea why CategoryID variable doesn't get any value and how can I fix the problem?
A server control parameter cannot contain a mixture of literal text and evaluated expressions.
The code you have will literally be posting back to AddItem.aspx?CategoryID=<%# Eval("CollectionID")%> and it will not be evaluating the code within the angle brackets.
You need to change your parameter like so
PostBackUrl='<%# "AddItem.aspx?CategoryID=" + Eval("CollectionID")%>' />
Here I have a DropDownList and Button ok that when clicked, it will enable visibility of other search textbox and search button. Also, this depend on what I select DropDownList item. Say, in my DropDownList, I have ProductName and ProductCode.I now select ProductName Next to this ddlist is button ok. When I click button ok, control such as label, textboxName and buttonSearchName will appear below of it.
How can I accomplish this?
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>ProductName</asp:ListItem>
<asp:ListItem>ProductCode</asp:ListItem>
<asp:ListItem>Category</asp:ListItem>
<asp:ListItem>SellingPrice</asp:ListItem>
<asp:ListItem>Quantity</asp:ListItem>
<asp:ListItem>BrandName</asp:ListItem>
<asp:ListItem>ReOrderQty</asp:ListItem>
<asp:ListItem>ReOrderLevel</asp:ListItem>
<asp:ListItem>Ordered</asp:ListItem>
<asp:ListItem>Allocated</asp:ListItem>
<asp:ListItem>FreeQty</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnOK" runat="server" onclick="btnOK_Click" Text="OK" />
<br />
ProductName<asp:TextBox ID="txtSearchProductname" runat="server"></asp:TextBox>
<asp:Button ID="btnSearchProductName" runat="server" Text="search"
onclick="btnSearchProductName_Click" />
<br />
To answer your question, one way to do it is (as answered before me) to add an update panel and set visibility to False, but then you would also require a ScriptManager and if you have other controls in the same page (such as a FileUpload control) that will not work properly with the presence of a ScriptManager.
You could alternatively use the same TextBox to search for all of the fields, by implementing a method that detects the value selected in your DropDownList and based on that value, the search algorithm changes accordingly.
So I just renamed your txtSearchProduct to just txtSearch and I added a universal method to search all criteria named btnSearch_Click
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>ProductName</asp:ListItem>
<asp:ListItem>ProductCode</asp:ListItem>
<asp:ListItem>Category</asp:ListItem>
<asp:ListItem>SellingPrice</asp:ListItem>
<asp:ListItem>Quantity</asp:ListItem>
<asp:ListItem>BrandName</asp:ListItem>
<asp:ListItem>ReOrderQty</asp:ListItem>
<asp:ListItem>ReOrderLevel</asp:ListItem>
<asp:ListItem>Ordered</asp:ListItem>
<asp:ListItem>Allocated</asp:ListItem>
<asp:ListItem>FreeQty</asp:ListItem>
</asp:DropDownList>
<br />
Search: <asp:TextBox ID="txtSearch" runat="server">
</asp:TextBox>
<asp:Button ID="btnSearch" runat="server" Text="search"
onclick="btnSearch_Click" />
<br />
and here is an example of what would the btnSearch_Click look like
protected void btnSearch_Click(object sender, EventArgs e)
{
string searchText = this.txtSearch.Text;
switch (this.DropDownList1.SelectedValue.ToString) {
case "ProductName":
string sql = "select * from products where ProductName like '%" + searchText + "%'";
// the rest of your code goes here
break;
case "ProductCode":
string sql = "select * from products where ProductCode like '%" + searchText + "%'";
// populate some other control with your productcode search here
break;
}
}
Lots of ways to do this, but since your just starting the easiest thing to do would be to put your controls in a panel and change the visibility in your "btnOK_Click" event.
Example:
<asp:Panel id="searchPanel" runat="server" visible="false">
your controls here....
</asp:Panel>
To make this visible, in your event use the following syntax.
searchPanel.Visible = True;