Link to home
Start Free TrialLog in
Avatar of newjeep19
newjeep19Flag for United States of America

asked on

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
Avatar of Ramkisan Jagtap
Ramkisan Jagtap
Flag of Finland image

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
Avatar of newjeep19

ASKER

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;      
                       }  
                   }
               
            }
        }
//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?
how r u setting text in the textbox? Are u fetching it from database?
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;
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
Can you give me an example or work with the code that i provided? Not sure if I understand.....thanks
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!!!
sorry its

 if (shpinfo=="")
                       {        
                           LinkButton imgBtn= (LinkButton)e.Row.FindControl("lnkShipHist");
                           imgBtn.Visible = false;      
                       }      
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?)      
sorry use e.RowIndex instead
Same error:
All I have for option is just e.Row not e.Row.Index or e.RowIndex
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;
}

e.RowIndex does work
Hey man!!
Give datakeynames="GTRI_Shippinginformationid" as u r comparing it in query
thanks
I changed the datakeynames and I get the same error:
Conversion failed when converting from a character string to uniqueidentifier.
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.
ASKER CERTIFIED SOLUTION
Avatar of Ramkisan Jagtap
Ramkisan Jagtap
Flag of Finland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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