I wanna put data I got from Repeater ( container.DataItem ) to function as Parameter.
This is what I have tried:
protected String getLink(string CustId)
{
string link = "";
if (Request.QueryString["mode"] != null)
{
link = "~/CustDetails.aspx?id="+CustId;
}
else
{
}
return link;
}
And here's my "HTML"
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<a href='<%# Response.Write(getLink(Eval(Container.DataItem,"CustId")))%>'>
<li><%# DataBinder.Eval(Container.DataItem,"Name")%></li>
You get The best overloaded method match for '' has some invalid arguments error because your function takes string as parameter, and you give it the object - the Eval() function returns an object. You can either use
protected String getLink(object CustId)
and convert it to string later, or the better way:
Response.Write(getLink(Eval(Container.DataItem,"CustId").ToString()))
That way you can keep your function unchanged.
You should use linkbutton inside repeater with
<asp:LinkButton ID ="asd" runat ="server" Text ='<%# DataBinder.Eval(Container.DataItem,"Name")%>' CommandArgument ='<%# Eval(Container.DataItem,"CustId")%>'></asp:LinkButton>
And set the OnItemCommand Property of repeater to call an event like this;
Response.Redirect("~/CustDetails.aspx?id="+Convert.toInt32(e.commandArgument));
Related
Why this is wrong?
<asp:Repeater ID="repeaterContent" runat="server">
<ItemTemplate>
<ArticleControlItem:ArticleControlItem runat="server"
Title='<%# Eval("Title") %>' ></ArticleControlItem:ArticleControlItem>
</ItemTemplate>
</asp:Repeater>
ArticleControlItem is my own Web Control, and I need to set parametr (Title). The control is placed in Repeater. How do I get variable "Title" from database to parametr Title? Why do I get this error?
CS0426: The type name ArticleControlItem does not exist in the type ArticleControlItem.ArticleControlItem.
This works perfect:
<asp:Repeater ID="repeaterContent" runat="server">
<ItemTemplate>
<ArticleControlItem:ArticleControlItem runat="server" Title="TEST" ></ArticleControlItem:ArticleControlItem>
</ItemTemplate>
</asp:Repeater>
Thank you.
Thank you for helping me. Relevant part of ArticleControlItem:
public string Title
{
get
{
object o = ViewState["Title"];
if (o == null) return "Default Title";
return (string)o;
}
set
{
ViewState["Title"] = value;
}
}
Can I save value from Eval("Title") to another variable? Something like
string TitleFromDB = Eval("Title")
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 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.
I have an image in a repeater in ASP.NET. I need to set the width of this image dynamically to a value returned from the database. I get the information from the SQL db, then I bind the repeater to the result set or datasource and I try to specify the width of the image in the repeater as follows:
<asp:Image ID="Image1" runat="server" Width='<%# Eval("ImageSize") %>' ImageUrl="~/Images/ProgressBar.jpg"/>
I get an error stating
Specified cast is not valid.
Could this be caused because of the datatype that is being returned from the db?
Use System.Web.UI.WebControls.Unit.Parse method:
<asp:Image
ID="Image1"
runat="server"
Width='<%# System.Web.UI.WebControls.Unit.Parse(Eval("ImageSize").ToString()) %>'
ImageUrl="~/Images/ProgressBar.jpg"/>
Re-Write in aspx file like this:
Width='<%# ConvertToImageSize(Eval("ImageSize")) %>'
Code-Behind:
protected int ConvertToImageSize(object imageSize)
{
int i = 0;
if (imageSize != null)
{
i = Convert.ToInt32(imageSize);
}
return i;
}
A bit rough but I hope you can do the rest of handling at your end easily.
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")%>' />