• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

How to hide a column in an ASP.NET Gridview if the textbox in the popup window does not contain data

I have a gridview. In my gridview I have a column that has a link button in each row below the column. When the user clicks the link a popup window pop's up. In that popup window there is a multilne textbox. If there is no data in that textbox in the popup window I want to disable the link or at least hide the column. I am new to ASP.NET and C# and i have no idea how to accomplish this. Below is the code that I have thus far:

ASP.NET (Link button):
   <asp:TemplateField HeaderText="Shipment History">
                  <ItemTemplate>
                  <asp:HyperLink ID="lnkShipHist" runat="server" ToolTip="Shippment Historty" NavigateUrl='<%# String.Format("http://localhost:57911/testpage1.aspx?id=", Eval("Gtri_ShippingHistory").ToString())%>' onclick="Popup=window.open('testpage1.aspx','Popup','toolbar=no, location=no,status=no,menubar=no,scrollbars=yes,resizable=no, width=320,height=240,left=430,top=23'); return false;">History... </asp:HyperLink>
                  </ItemTemplate>
                  </asp:TemplateField>

Open in new window


C# Code:
No code for the link button
0
newjeep19
Asked:
newjeep19
  • 11
  • 8
1 Solution
 
Ramkisan JagtapLead DeveloperCommented:
write a function to check if text is present and call it on rowdatabound event of grid view. If text is not available disable the hyperlink. Refer link below
Http://www.stackoverflow.com/questions/6936754/hide-image-button-from-some-rows-in-gridview
0
 
newjeep19Author Commented:
Not sure what the condtion should be.
what I have so far........in C#
  protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                            //Find control of hyperlink Add hidden fild that has shipping history check the value=lable.text != or null hide the hyperlink e  
                   if (e.Row.RowType == DataControlRowType.DataRow)
                   {
                       if (txtDateTime.Text !=)
                       {        
                           LinkButton imgBtn= (LinkButton)e.Row.FindControl("lnkShipHist");
                           imgBtn.Visible = false;      
                       }  
                   }
               
            }
        }
0
 
newjeep19Author Commented:
//Find control of hyperlink Add hidden fild that has shipping history check the value=lable.text != or null hide the hyperlink e  
                if (testpage1.txtDateTime != null)
                {
                    LinkButton lnkBtn = (LinkButton)e.Row.FindControl("lnkShipHist");
                    lnkBtn.Visible = false;      
                }  
testpage1 is the name of the popup window where the textbox is at.....called txtDateTime
So, i don't understatnd how to check the textbox that is in the popup window if it has data in it?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ramkisan JagtapLead DeveloperCommented:
how r u setting text in the textbox? Are u fetching it from database?
0
 
newjeep19Author Commented:
Yes.........here is the C# code that i am using
 if (!String.IsNullOrEmpty(Request.QueryString["id"]))

            {
                string ShippingHistory = Request.QueryString["id"].ToString();
                string strSQL = @"SELECT [Gtri_ShippingHistory] FROM [GTRI_shippinginformation] WHERE GTRI_shippinginformationId = '" + ShippingHistory +"'";

                string ShippingInfo =String.Empty;
                string mConnection = ConfigurationManager.ConnectionStrings["CRMConnectionInfo"].ConnectionString;
                using (SqlConnection oConn = new SqlConnection(mConnection))
                {
                    oConn.Open();
                    using (SqlCommand cmd = new SqlCommand(strSQL, oConn))
                    {
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                ShippingInfo = reader.GetString(0);
                                break;

                            }
                        }
                    }
                }
                txtDateTime.Text = ShippingInfo;
0
 
Ramkisan JagtapLead DeveloperCommented:
write same code in a function which will return the text which u set in textbox. Get that in a string. Compare that string for condition
0
 
newjeep19Author Commented:
Can you give me an example or work with the code that i provided? Not sure if I understand.....thanks
0
 
Ramkisan JagtapLead DeveloperCommented:
Write below function in your griedview page

protected string CheckForText(string id)
{
      string ShippingHistory = id;
                string strSQL = @"SELECT [Gtri_ShippingHistory] FROM [GTRI_shippinginformation] WHERE GTRI_shippinginformationId = '" + ShippingHistory +"'";

                string ShippingInfo =String.Empty;
                string mConnection = ConfigurationManager.ConnectionStrings["CRMConnectionInfo"].ConnectionString;
                using (SqlConnection oConn = new SqlConnection(mConnection))
                {
                    oConn.Open();
                    using (SqlCommand cmd = new SqlCommand(strSQL, oConn))
                    {
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                ShippingInfo = reader.GetString(0);
                                break;

                            }
                        }
                    }
                }
               return ShippingInfo;
}

Add datakey to your gridview on aspx page as below
datakeynames="Gtri_ShippingHistory"

Example : <asp:gridview id="CustomersGridView"      
        datakeynames="CustomerID"    
        runat="server">

On your row databound event check as below

protected void gvParent_RowDataBound(object sender, GridViewRowEventArgs e)
        {
           

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                          string id= gvParent.DataKeys[e.Row.Index].Value.ToString();
                   string shpinfo= CheckForText(id);
                 
                       if (shpinfo!=="")
                       {        
                           LinkButton imgBtn= (LinkButton)e.Row.FindControl("lnkShipHist");
                           imgBtn.Visible = false;      
                       }          
               
            }
        }

Thanks!!!
0
 
Ramkisan JagtapLead DeveloperCommented:
sorry its

 if (shpinfo=="")
                       {        
                           LinkButton imgBtn= (LinkButton)e.Row.FindControl("lnkShipHist");
                           imgBtn.Visible = false;      
                       }      
0
 
newjeep19Author Commented:
No problem however, I am running into another issue:

 string id = gvParent.DataKeys[e.Row.Index].Value.ToString();
e.Row.Index i get this error:
System.Web.UI.WebControls.GridViewRow' does not contain a definition for 'Index' and no extension method 'Index' accepting a first argument of type 'System.Web.UI.WebControls.GridViewRow' could be found (are you missing a using directive or an assembly reference?)      
0
 
Ramkisan JagtapLead DeveloperCommented:
sorry use e.RowIndex instead
0
 
newjeep19Author Commented:
Same error:
All I have for option is just e.Row not e.Row.Index or e.RowIndex
0
 
newjeep19Author Commented:
I get this error:
Conversion failed when converting from a character string to uniqueidentifier.

in this pice:
protected string CheckForText(string id)
{
      string ShippingHistory = id;
                string strSQL = @"SELECT [Gtri_ShippingHistory] FROM [GTRI_shippinginformation] WHERE GTRI_shippinginformationId = '" + ShippingHistory +"'";

                string ShippingInfo =String.Empty;
                string mConnection = ConfigurationManager.ConnectionStrings["CRMConnectionInfo"].ConnectionString;
                using (SqlConnection oConn = new SqlConnection(mConnection))
                {
                    oConn.Open();
                    using (SqlCommand cmd = new SqlCommand(strSQL, oConn))
                    {
                        using (SqlDataReader reader = cmd.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                ShippingInfo = reader.GetString(0);
                                break;

                            }
                        }
                    }
                }
               return ShippingInfo;
}

0
 
newjeep19Author Commented:
e.RowIndex does work
0
 
Ramkisan JagtapLead DeveloperCommented:
Hey man!!
Give datakeynames="GTRI_Shippinginformationid" as u r comparing it in query
thanks
0
 
newjeep19Author Commented:
I changed the datakeynames and I get the same error:
Conversion failed when converting from a character string to uniqueidentifier.
0
 
newjeep19Author Commented:
It errors out by in the
  while (reader.Read())
                            {
                                ShippingInfo = reader.GetString(0);
                                break;

                            }
part of the code above

error:
Conversion failed when converting from a character string to uniqueidentifier.
0
 
Ramkisan JagtapLead DeveloperCommented:
what is the datatype of GTRI_Shippinginformationid in database?
Please debug the code and check for the value you pass to the function"CheckForText"?
check if it is a GUID?
If yes, you need to convert that to "uniqueidentifier" explicitely in the query.

Regards,
Ram
0
 
newjeep19Author Commented:
Yes GTRI_Shippinginformationid is a GUID ........ I don't know how to convert it to a "uniqueidentifier" in a query. Any help is much appricated
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 11
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now