newjeep19
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):
C# Code:
No code for the link button
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>
C# Code:
No code for the link button
ASKER
Not sure what the condtion should be.
what I have so far........in C#
protected void gvParent_RowDataBound(obje ct 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.FindCont rol("lnkSh ipHist");
imgBtn.Visible = false;
}
}
}
}
what I have so far........in C#
protected void gvParent_RowDataBound(obje
{
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.FindCont
imgBtn.Visible = false;
}
}
}
}
ASKER
//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.FindCont rol("lnkSh ipHist");
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?
if (testpage1.txtDateTime != null)
{
LinkButton lnkBtn = (LinkButton)e.Row.FindCont
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?
ASKER
Yes.........here is the C# code that i am using
if (!String.IsNullOrEmpty(Req uest.Query String["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.Conne ctionStrin gs["CRMCon nectionInf o"].Connec tionString ;
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;
if (!String.IsNullOrEmpty(Req
{
string ShippingHistory = Request.QueryString["id"].
string strSQL = @"SELECT [Gtri_ShippingHistory] FROM [GTRI_shippinginformation]
string ShippingInfo =String.Empty;
string mConnection = ConfigurationManager.Conne
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
ASKER
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.Conne ctionStrin gs["CRMCon nectionInf o"].Connec tionString ;
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_Shippin gHistory"
Example : <asp:gridview id="CustomersGridView"
datakeynames="CustomerID"
runat="server">
On your row databound event check as below
protected void gvParent_RowDataBound(obje ct sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow )
{
string id= gvParent.DataKeys[e.Row.In dex].Value .ToString( );
string shpinfo= CheckForText(id);
if (shpinfo!=="")
{
LinkButton imgBtn= (LinkButton)e.Row.FindCont rol("lnkSh ipHist");
imgBtn.Visible = false;
}
}
}
Thanks!!!
protected string CheckForText(string id)
{
string ShippingHistory = id;
string strSQL = @"SELECT [Gtri_ShippingHistory] FROM [GTRI_shippinginformation]
string ShippingInfo =String.Empty;
string mConnection = ConfigurationManager.Conne
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_Shippin
Example : <asp:gridview id="CustomersGridView"
datakeynames="CustomerID"
runat="server">
On your row databound event check as below
protected void gvParent_RowDataBound(obje
{
if (e.Row.RowType == DataControlRowType.DataRow
{
string id= gvParent.DataKeys[e.Row.In
string shpinfo= CheckForText(id);
if (shpinfo!=="")
{
LinkButton imgBtn= (LinkButton)e.Row.FindCont
imgBtn.Visible = false;
}
}
}
Thanks!!!
sorry its
if (shpinfo=="")
{
LinkButton imgBtn= (LinkButton)e.Row.FindCont rol("lnkSh ipHist");
imgBtn.Visible = false;
}
if (shpinfo=="")
{
LinkButton imgBtn= (LinkButton)e.Row.FindCont
imgBtn.Visible = false;
}
ASKER
No problem however, I am running into another issue:
string id = gvParent.DataKeys[e.Row.In dex].Value .ToString( );
e.Row.Index i get this error:
System.Web.UI.WebControls. GridViewRo w' does not contain a definition for 'Index' and no extension method 'Index' accepting a first argument of type 'System.Web.UI.WebControls .GridViewR ow' could be found (are you missing a using directive or an assembly reference?)
string id = gvParent.DataKeys[e.Row.In
e.Row.Index i get this error:
System.Web.UI.WebControls.
sorry use e.RowIndex instead
ASKER
Same error:
All I have for option is just e.Row not e.Row.Index or e.RowIndex
All I have for option is just e.Row not e.Row.Index or e.RowIndex
ASKER
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.Conne ctionStrin gs["CRMCon nectionInf o"].Connec tionString ;
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;
}
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]
string ShippingInfo =String.Empty;
string mConnection = ConfigurationManager.Conne
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;
}
ASKER
e.RowIndex does work
Hey man!!
Give datakeynames="GTRI_Shippin ginformati onid" as u r comparing it in query
thanks
Give datakeynames="GTRI_Shippin
thanks
ASKER
I changed the datakeynames and I get the same error:
Conversion failed when converting from a character string to uniqueidentifier.
Conversion failed when converting from a character string to uniqueidentifier.
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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
Http://www.stackoverflow.com/questions/6936754/hide-image-button-from-some-rows-in-gridview