English 中文(简体)
ASP.NET 2.0 C#- A couple issues with a GridView inside a UserControl
原标题:

I have an asp.net page with a custom usercontrol which contains a selectable gridview, and a formview which is on the page directly.

First issue: I want the FormView s pageindex to be the selected index of the GridView. I can get the selectedindex of the GridView by doing this:

public virtual int SelectedIndex
    {
        get { return this.GridView1.SelectedIndex; }
        set { this.GridView1.SelectedIndex = value; }
    }

Then on my aspx page I can do this:

FormView1.PageIndex = CodeView1.SelectedInex;

The problem is that I can t seem to figure out how to call this function from within my aspx page for whenever I click the Select link of a row on my gridview. I ve assigned it to a button onclick just to make sure it works, and it does. I d just like to be able to do it from the gridview directly.

Second Issue

On the gridview (the same one as above), I have an itemtemplate for the select command which is an asp:linkbutton. Whenever I click the Select link on the gridview, all the formatting that I ve set up in the rowdatabound function gets undone.

For example, I have this in my rowdatabound:

HyperLink TicketDetailLink = new HyperLink();
TicketDetailLink.NavigateUrl = "TicketDetail.aspx?TicketNumber=" + TicketNumber;
TicketDetailLink.Text = TicketNumber;
e.Row.Cells[5].Controls.Add(TicketDetailLink);

But as soon as I click my select link, that hyperlink disappears. Thoughts?

问题回答

First Issue:

        string key;

        LinkButton myLink = (LinkButton)sender;
        GridViewRow gridview = (GridViewRow)myLink.Parent.Parent;

*Assign2Control*= gvManagePlanning.DataKeys[gridview.RowIndex].Value.ToString();.

This is how i get the value from my gridview. Add a templatefield to you gridview and put a linkbutton in it. Put this code in the onclick event. Add the value you want to retrieve to the datakeynames so it gets stored in the Datakeys. You will have to change the datakey index if there is more than one datakeys.

Second Issue:

      <asp:HyperLink ID="HyperLink1" runat="server" 
NavigateUrl= <%# Eval("TicketNumber", "TicketDetail.aspx?TicketNumber={0}") %> 
    Text="TicketNumber"></asp:HyperLink> 




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签