locked
passing id to Gridview RRS feed

  • Question

  • User1696625220 posted

    Hi, I'm want to pass an id to a gridview that contains a Select link. Once I select a row, that rows ID would return/replace the one passed. I think I've seen this done before but I can't find an example at this time. Please send a link or example if you have one handy. Thx.

    Wednesday, September 19, 2012 12:32 PM

Answers

  • User3866881 posted

    Hello:)

    You can just use HyperLink to bind to a ID to generate the url link in the GridView, and then when someone clicks that, it will guide him/her to somewhere else and in that page please use Request.QueryString to accept the value and fetch or query what you need.

    To change the NavigateUrl:

    NavigateUrl='<%# Eval("SomeIDfield", "xxx.aspx?id={0}"%>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 20, 2012 9:46 PM
  • User1696625220 posted

    I think I will just use a SPROC for this last step. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 21, 2012 10:41 AM
  • User1696625220 posted

    Here we go, this works....

            protected void FilterByConvent(object sender, CustomExpressionEventArgs e)
            {
                string ConvID = Request.QueryString["ConventID"].ToString();
                int convNum = Convert.ToInt32(ConvID);
                e.Query = from p in e.Query.Cast<Residence>()
                          where p.ConventID == convNum
                          select p;
            }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 8, 2012 12:46 PM

All replies

  • Thursday, September 20, 2012 3:09 AM
  • User3866881 posted

    Hello:)

    You can just use HyperLink to bind to a ID to generate the url link in the GridView, and then when someone clicks that, it will guide him/her to somewhere else and in that page please use Request.QueryString to accept the value and fetch or query what you need.

    To change the NavigateUrl:

    NavigateUrl='<%# Eval("SomeIDfield", "xxx.aspx?id={0}"%>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 20, 2012 9:46 PM
  • User1696625220 posted

    Thanks DD,

    Here's how I solved my first issue...

                      <asp:hyperlinkfield headertext="Convent"
                        datanavigateurlfields="SisterID" 
                        datanavigateurlformatstring="/archives/Convent/ConventSelectList.aspx?SisterID={0}" Text="Change Assigned Convent" />
    

    Now that link takes the user to a custom page with a list of convents to choose from. Each row has a Select link. I'm now trying to figure out how to use the ExecuteStoreCommand to replace the ConventID in the Residence table with the ConventID selected from the list. (It's important that I use a list versus a dropdownlist so the user can view the entire address). I'm unclear on the connectionString - why it doesn't seem to recgonize my code.

        sistersDBEntities se = new sistersDBEntities();
        EntityConnection econn = (EntityConnection)se.Connection;
        SqlConnection sconn = (SqlConnection)econn.StoreConnection;

            protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
            {
                // Retrieve ConventID from the appropriate cell.
                String selectedConventID = GridView1.SelectedRow.Cells[1].Text;
                String sisID = Request.QueryString["SisterID"];
                Message.Text = "Convent ID: " + selectedConventID;
                Message2.Text = "Sister ID: " + sisID;
                
                SqlCommand cmd = new SqlCommand("select CategoryName from Categories", conn);
                se.ExecuteStoreCommand("UPDATE Convent SET ConventID = selectedConventID WHERE SisterID = sisID");                
                se.SaveChanges();
    
            }
    




    Friday, September 21, 2012 8:37 AM
  • User1696625220 posted

    After reading this article: http://cgeers.com/2009/02/21/entity-framework-objectcontext/

    I tried this approach but I'm getting an error....

            public class SharedObjectContext
            {
                private readonly sistersDBEntities context;
    
                #region Singleton Pattern
    
                // Static members are lazily initialized.
                // .NET guarantees thread safety for static initialization.
                private static readonly SharedObjectContext instance = new SharedObjectContext();
    
                // Make the constructor private to hide it. 
                // This class adheres to the singleton pattern.
                private SharedObjectContext()
                {
                    // Create the ObjectContext.
                    context = new sistersDBEntities();
                }
    
                // Return the single instance of the ClientSessionManager type.
                public static SharedObjectContext Instance
                {
                    get
                    {
                        return instance;
                    }
                }
    
                #endregion
    
                public sistersDBEntities Context
                {
                    get
                    {
                        return context;
                    }
                }
            }
            
            protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
            {
                // Retrieve ConventID from the appropriate cell.
                String selectedConventID = GridView1.SelectedRow.Cells[1].Text;
                String sisID = Request.QueryString["SisterID"];
                Message.Text = "Convent ID: " + selectedConventID;
                Message2.Text = "Sister ID: " + sisID;
    
                sistersDBEntities context = SharedObjectContext.Instance.Context;
    
                context.ExecuteStoreCommand("UPDATE Convent SET ConventID = selectedConventID WHERE SisterID = sisID");
                context.SaveChanges();
    
            }

    Compiler Error Message: CS1061: 'ASP.dynamicdata_custompages_convent_conventselectlist_aspx' does not contain a definition for 'DataSource_Selected' and no extension method 'DataSource_Selected' accepting a first argument of type 'ASP.dynamicdata_custompages_convent_conventselectlist_aspx' could be found (are you missing a using directive or an assembly reference?)
    
    Source Error:
    
     
    
    Line 64:             <asp:label id="Message2" forecolor="Red" runat="server"/><br />
    Line 65: 
    Line 66:             <asp:EntityDataSource ID="GridDataSource" runat="server" 
    Line 67:                 OnSelected="DataSource_Selected" Include="Convent" 
    Line 68:                 ConnectionString="name=sistersDBEntities" 
     
    
    Source File: c:\inetpub\archivesDB\DynamicData\CustomPages\Convent\ConventSelectList.aspx    Line: 66 
    



    I think this approach is messing up the EntityDataSource.

    Friday, September 21, 2012 9:41 AM
  • User1696625220 posted

    I think I will just use a SPROC for this last step. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 21, 2012 10:41 AM
  • User-179079940 posted

    use button then

    commandargument='<%#Eval("your id field name")%>

    Monday, October 8, 2012 9:16 AM
  • User1696625220 posted

    I think I have found a solution to my problem using QueryExtender - CustomExpression....

    here's the gridview with QueryExtender:

                <asp:GridView ID="GridView1" runat="server" DataSourceID="GridDataSource" 
                        AutoGenerateColumns="False">
                    <Columns>
                        <asp:ImageField  ShowHeader="true" DataImageUrlField="SisterID" DataImageUrlFormatString="http://www.cscsisters.org/Members/SistersDirectoryPhotos/_t/{0}0_jpg.jpg" HeaderText="Image"></asp:ImageField>
                        <asp:BoundField DataField="SisterID" HeaderText="SisterID" ReadOnly="True" 
                            SortExpression="SisterID" />
                        <asp:BoundField DataField="ConventID" HeaderText="ConventID" 
                            ReadOnly="True" SortExpression="ConventID" />
                        <asp:BoundField DataField="SoloGroup" HeaderText="SoloGroup" ReadOnly="True" 
                            SortExpression="SoloGroup" />
                        <asp:BoundField DataField="LocalCommunity" HeaderText="LocalCommunity" 
                            ReadOnly="True" SortExpression="LocalCommunity" />
                    </Columns>
                </asp:GridView>
    
                <asp:EntityDataSource ID="GridDataSource" runat="server" EnableDelete="true" EnableUpdate="true" />
    
                <asp:QueryExtender TargetControlID="GridDataSource" ID="GridQueryExtender" runat="server">
                    <asp:CustomExpression OnQuerying="FilterByConvent"></asp:CustomExpression>
                </asp:QueryExtender>
    

    and code behind:

            protected void FilterByConvent(object sender, CustomExpressionEventArgs e)
            {
     
                e.Query = from p in e.Query.Cast<Residence>()
                          where p.ConventID == (??? CONVENT ID PARAMETER ???)
                          select p;
            }
            
    

    My URL looks like this:

    http://webapps/archives/Residence/ListDetails.aspx?ConventID=4232&SisterID=1115

    How do I retrieve the ConventID parameter I'm passing?


    Monday, October 8, 2012 10:11 AM
  • User1696625220 posted

    Here we go, this works....

            protected void FilterByConvent(object sender, CustomExpressionEventArgs e)
            {
                string ConvID = Request.QueryString["ConventID"].ToString();
                int convNum = Convert.ToInt32(ConvID);
                e.Query = from p in e.Query.Cast<Residence>()
                          where p.ConventID == convNum
                          select p;
            }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 8, 2012 12:46 PM