In my web page i used a gridview. In this gridview it shows a group of users information.
I just added one button from smart tag menu. And my requirement is that when i am hitting the button corresponding to each users, it will redirect to another page and shows the corresponding user's information. What i do for getting this type of output?
U have to add the button and add an attribute CommandName:
<asp:Button ID="EditBtn" runat="server" CommandName="Edit" />
then in the event of itemcommand of the grid do the following
protected void GridView1_ItemCommand(object source, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
//Redirect to user page information
Response.Redirect(PageURL);
}
}
Ahmy's answer is the way to go if you want to use a button and have your page redirect to another page with the user's information. One thing that was left out however was that you can pass a command argument through the button (like the user's unique ID) which you could then put in the querystring of the page you are redirecting to, to identify which user it is. That would look like this:
<asp:TemplateField HeaderText="Edit User">
<ItemTemplate>
<asp:Button ID="EditBtn" Text="Edit User" CommandName="Edit"
CommandArgument='<%# Eval("UserID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
Then in the code behind
protected void GridView1_ItemCommand(object source, GridViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
//Redirect to user page information
Response.Redirect("UserProfilePage.aspx?userID=" + e.CommandArgument);
}
}
Another alternative to using a button, which I think is the best option is to use a HyperLinkField. When using a button, the page will have to post back to the server, then send a redirect to the user's browser. With a hyperlink, the user goes straight to the correct page. It saves a step and doesn't rely on javascript.
<asp:HyperLinkField DataTextField="UserName" DataNavigateUrlFields="UserID"
DataNavigateUrlFormatString="UserProfilePage.aspx?userID={0}"
HeaderText="Edit User" />
Instead of button, make one of the column hyperlinked. On clicking the item, redirect to your new page (using Javascript). By this you can avoid an additional column for the button and a postback.
You must use DataTextFormatString for this.
Sample
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="no" HeaderText="SNo" />
<asp:BoundField DataField="file" DataFormatString="<a href=javascript:ShowAssetDetail('{0}');>{0}</a>"
HeaderText="Asset Desc" HtmlEncodeFormatString="False" />
</Columns>
</asp:GridView>
In above sample the JS function ShowAssetDetail() must take a value to pass to the redirecting page. Needless to say, the JS function must be written in addition.
Related
Here is my code:
<form id="form1" runat="server">
<asp:GridView ID="gridv" AutoGenerateColumns="true" EnableViewState="true" runat="server" >
<Columns>
<asp:HyperLinkField runat="server" HeaderText="GetStudentInfo" SortExpression="GetStudentInfo" DataTextField="StudentName" NavigateUrl="StudentManagement2.aspx" />
</Columns>
</asp:GridView>
<asp:Button runat="server" Text="Click" OnClick="ClickPostback" />
</form>
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack) return;
gridv.DataSource = GetStudent();
gridv.DataBind();
}
When i click on the hyperlink on GridView - Postback is always false:
However when i click the Button Postback variable is True:
A post back occurs when a form gets submitted back to the server.
Hyperlinks are used for navigation, not form submission. So by default they redirect the user to a new page and do not post any information back to the server, which is why the post back is showing as false.
In your example the hyperlink has a NavigateUrl property which is where you are telling the application to "go to this page". It's not sending any information to the server for processing.
Buttons however where designed to post information back to the server, which is why it is showing true.
I have build a GridView on a webpage, having two ButtonFields columns: Accept and Reject. Now when I click on Reject button, I want to show a popup form with some input fields and store that data into the Database.
I'm just stuck at showing the popup on clicking the reject button field. Is this possible?v
put code in .aspx file
<asp:TemplateField HeaderText="Edit" itemstyle-width="150px">
<ItemTemplate>
<asp:LinkButton ID="btnEdit" runat="server" CommandName="Edit" Text="Edit" CausesValidation="false"/>
</ItemTemplate>
</asp:TemplateField>
put code in .cs file
if (e.CommandName.Equals("Edit"))
{
string QueryString="val";
Page.ClientScript.RegisterStartupScript(GetType(), "", "window.open('Page.aspx?QS=" + QueryString + "','','width=500,height=500');"", true);
}
I've written an ASP code for a webshop type project. There is a method that adds the ProductID to a cookie and adds 1 to the quantity of the product.
Another method reads the cookie, transforms the cookie to a data table and puts it in a gridview. This gridview basically is the shoppingcart.aspx page.
What i now need is a way to add a buttonfield to that gridview where i can add a ++ button (like the add to shopping cart button on a product page), normal asp buttons take a onclick="methodname" but these in the gridview don't.
add1ToShoppingCart(string productId)
{[...]shoppingCartCookie[productId] = (amount + 1).ToString();}
That is the code I use for all the ++ buttons. It takes the button.id (buttonID=productID). So I need a way to have all the buttons be the same image, but the buttonID has to be databound to the productID so it can somehow execute that method (with some sort of onclick="").
I've looked and googled for the past couple hours but I cant seem to find anything. In the past I found a lot of answers on stackoverflow so I hoped somebody here could help.
Thanks in advance.
you can use Template field :
<asp:TemplateField ItemStyle-Width="33px" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle"
ShowHeader="false" HeaderStyle-Height="40px">
<ItemTemplate>
<asp:ImageButton ID="btnAddToCard" runat="server" ImageUrl="~/Images/btnAddToCard.png" CausesValidation="false"
CommandName="AddToCard" CommandArgument='<%# Eval("ID") %>'
/>
</ItemTemplate>
<HeaderStyle Height="40px"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="33px"></ItemStyle>
</asp:TemplateField>
and in your C# code behind you create the following event of your gridview and do what you want :
protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "AddToCard")
{
//Write code to add to card
}
}
GV is the ID of my GridView !
I have an ImageButton within a GridView in .aspx on clicking this ImageButton i have to call a function.
This is how i tried and the function was not being called.
Code inside.aspx page:
<GridView ......>
<asp:HyperLink ID="HyperLink2" runat="server"
NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"VehID","mngVeh.aspx?delid={0}") %>'>
<asp:ImageButton runat="server" ID="DeleteUrlImageButton"
width='24' height='24'
ImageUrl="~/images/delete.jpeg"
OnClick="DeleteUrlImageButton_Click"
OnClientClick="return confirm('Are you sure you want to delete?');" />
<!--<img src="images/delete.jpeg" alt='edit' border="0" width='24' height='24'/> -->
</asp:HyperLink>
</GridView>
code in .aspx.cs page:
public void DeleteUrlImageButton_Click(object sender, EventArgs e)
{
//code to perform the necessary action.
}
Because you are wrapping your ImageButton inside of a Hyperlink, the browser is probably going to the hyperlink's URL instead of posting back to hit the OnClick function. You should have the DeleteUrlImageButton_Click function call Server.Transfer or Response.Redirect to the appropriate URL and get rid of the Hyperlink.
Sure it won't be fired because it is nested in a Hyperlink. So the imagebutton serves as the text for the hperlink and hyperlink does not cause postback. ImageButton can cause the desired action only if it stands out of the Hyperlink. Try this:
<asp:GridView ....
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:ImageButton runat="server" ID="DeleteUrlImageButton"
width='24' height='24'
ImageUrl="~/images/delete.jpeg"
OnClick="DeleteUrlImageButton_Click"
OnClientClick="return confirm('Are you sure you want to delete?');"
PostBackUrl='<%# DataBinder.Eval(Container.DataItem,"VehID","mngVeh.aspx?delid={0}")
%>'/>
</ItemTemplate>
</asp:TemplateField>
</asp:GridView>
ImageButton can do the job no need for Hyperlink just use the postbackurl and it will redirect you to the page. You can omit the HyperLink.
Button controls (like LinkButton,ImageButton and Button) are designed to cause postback by default.
Edit: Make sure event name and arguments are correct. This is the event I used to test it. y the way don't forget to place the ImageButton in a TemplateField, refer the code above
protected void DeleteUrlImageButton_Click(object sender, ImageClickEventArgs e)
{
TextBox5.Text = "Fired ";
//Response.Redirect( ((ImageButton)sender).PostBackUrl);//uncomment this if the button does not automatically redirects. This line should be the last one
}
I am new to ASP.net thing, and i have some question regarding postback.
I have a Senario like this:
1) I have a grid on web with a panel inside.
2) I "Insert" the panel with a Web User Control by calling this
Control ctlControl;
ctlControl = LoadControl("~/UserControls/ChequeCreation.ascx");
pnlTransaction.Controls.Add(ctlControl);
3)The Web User Control providing two button. One is "update" and one is "reset".
Problem is like here:
What i wanted to achieve is when press the "update" button, it will update something back to my DB? But seem after i press the button "Update" or "Reset". The web user control is gone or missing. For my guest is because of the postback issues? Is that correct?
I tried if(!postback) still its doesn't work.
How am i going to overcome this? I already scratching my head about a day?
Thanks you so much.
Regards
LiangCk:
PS:Sorry for my english level, and please dont hesitate to voice out my error or mistake.
well you can convert any of your data columns to template column and then drag and drop your web user control to it
this will result in something like the following code check where "uc1:webUserControle1" is located in the code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="SqlDB">
<Columns>
<asp:TemplateField HeaderText="ID" SortExpression="ID">
<EditItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>'></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
<uc1:webUserControle1 ID="WebUserControle1_1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
</Columns>
</asp:GridView>
if you are using AJAX, try add updatepanel on your UCT design page
ASP.NET will not preserve a dynamically added user control between postbacks. This is why it is dissapearing. You will need to add the control each time the page is created. However you will need to add it when the control tree is being initialized and restore the original control ID if you want your events to fire. These links provide a full explanation https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx and http://avinashsing.sunkur.com/2011/02/24/dynamic-controls-viewstate-and-postback/
You have to Every Time Reload the user control on Page_Init or
Page_Load. Then you can get the Button Click Event and After tha User
Control will not lost.
private void LoadUserControl(){
string controlPath = LastLoadedControl;
if (!string.IsNullOrEmpty(controlPath)) {
PlaceHolder1.Controls.Clear();
UserControl uc = (UserControl)LoadControl(controlPath);
PlaceHolder1.Controls.Add(uc);
}
}
protected void Page_Load(object sender, EventArgs e) {
LoadUserControl();
}