Question

This seems to be a common question but somehow none of the answers I found seem to fix my case - so wondering what I might be missing (I'm new to asp.net so please pardon any blatant mistakes).

I have a GridView that is bound to an ObjectDataSource that displays paper magazines and their subscription prices. If the user wishes to subscribe to any magazine, I'd like to add a textbox and a button to each row in the grid to allow the user to enter a date and click the button to subscribe. No field exists in GridView for this textbox.

Ideally, I would like to add a parameter to UpdateParameters to send the value in the textbox to the Update method. But I have been unsuccessful in doing so.

Some suggestions I found on using FindControl is not working either. (I see using Inspect Element in Firefox that the control ID generated has a Ctrl0, Ctrl1, Ctrl2 prefixes.

Attached is the sample ASPX page and the code behind. Deeply appreciate any insights on what I'm missing here.

Many thanks in advance.

Here's the ASPX file and the code behind:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestGridView.aspx.cs" Inherits="WebApplication1.TestGridView" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server" DatasourceID="ObjectDataSource2" DataKeyNames ="MagazineID" OnRowUpdating="GridView1_RowUpdating">
        <Columns>
            <asp:BoundField DataField="MagazineID" HeaderText ="Magazine ID" ReadOnly="true" SortExpression="PricingID">
                <ControlStyle Width="100%" />
            </asp:BoundField>
            <asp:BoundField DataField="MagazineName" HeaderText ="Magazine Name" ReadOnly="true" />
            <asp:BoundField DataField="MagazinePrice" HeaderText ="Magazine price" ReadOnly="true" />
            <asp:TemplateField HeaderText ="SubscriptionStartDate">
                <ItemTemplate>
                    <asp:TextBox ID ="txtSubscriptionStartDateStr" runat="server" Width ="100px"/>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:ButtonField ButtonType="Button" CommandName="Update" Text="Subscribe" />
        </Columns>
    </asp:GridView>

    <asp:ObjectDataSource ID="ObjectDataSource2" runat="server" UpdateMethod ="AddNewSubscription" SelectMethod="GetAllMagazines" TypeName="WebApplication1.TestGridView">
        <UpdateParameters>
            <asp:ControlParameter ControlID="txtSubscriptionStartDateStr" DefaultValue="" Name="userName" PropertyName="Text" Type="string" />
        </UpdateParameters>
    </asp:ObjectDataSource>

    </div>
    </form>
</body>
</html>

Code-Behind:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class TestGridView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public DataTable GetAllMagazines()
        {
            DataTable dt = new DataTable();

            object[] parms1 = new object[] {11, "Magazine1", 5.99};
            object[] parms2 = new object[] {12, "Magazine2", 3.99 };

            dt.Columns.Add("MagazineID");
            dt.Columns.Add("MagazineName");
            dt.Columns.Add("MagazinePrice");

            dt.Rows.Add(parms1);
            dt.Rows.Add(parms2);

            return dt;
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            GridView gv = (GridView)sender;
            GridViewRow gvRow = gv.Rows[e.RowIndex];
            TextBox tb = (TextBox) gv.FindControl("txtSubscriptionStartDateStr");
            if (tb == null)
                throw new ApplicationException("Could not find TextBox");

            string subscriptionStartDateStr = tb.Text;
            // more code to parse and use the subscription date
        }

        public void AddNewSubscription(int magazineID, string subscriptionStartDateStr)
        {
            // insert database row here.

        }
    }
}
Was it helpful?

Solution

You almost have it, you are calling the FindControl on the wrong object.

In your GridView1_RowUpdating method change:

TextBox tb = (TextBox) gv.FindControl("txtSubscriptionStartDateStr");

to

TextBox tb = (TextBox) gvRow.FindControl("txtSubscriptionStartDateStr");

So your function is this:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
  GridView gv = (GridView)sender;
  GridViewRow gvRow = gv.Rows[e.RowIndex];
  TextBox tb = (TextBox) gvRow.FindControl("txtSubscriptionStartDateStr");
  if (tb == null)
     throw new ApplicationException("Could not find TextBox");

  string subscriptionStartDateStr = tb.Text;
  // more code to parse and use the subscription date
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top