I wanted to display value in a label (label inside itemTemplate)
i have tried this approach
string s = null;
SelfCollectNameSpace.SelfCollect address = new SelfCollectNameSpace.SelfCollect();
s = address.getSelfCollectAddress(orderNoTracking);
if (string.IsNullOrEmpty(s) == false)
{
foreach (GridViewRow row in GridView1.Rows)
{
string LabelText = ((Label)row.FindControl("labelSelf")).Text;
LabelText = s;
Response.Write(LabelText+"test");
}
}
but nothing is shown in the label inside the gridview. IT should display the value of s.
Next approach was
string s = null;
SelfCollectNameSpace.SelfCollect address = new SelfCollectNameSpace.SelfCollect();
s = address.getSelfCollectAddress(orderNoTracking);
Label labelSelfCollect = GridView1.FindControl("labelSelf") as Label;
labelSelfCollect.Text = "Self Collect at "+ s;
I got this error
System.NullReferenceException: Object reference not set to an instance of an object.
and the last approach that i have used is
string s = null;
SelfCollectNameSpace.SelfCollect address = new SelfCollectNameSpace.SelfCollect();
s = address.getSelfCollectAddress(orderNoTracking);
if (string.IsNullOrEmpty(s) == false)
{
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
Label labelcollect = row.FindControl("labelSelf") as Label;
labelcollect.Text = "self collect at "+ s;
}
}
}
using this method also it shows nothing.How should i assign the s value into this labelSelf(label inside itemTemplate)?
my aspx code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="772px" style="margin-left: 120px; text-align: left" Height="100px" DataKeyNames ="progressID" OnRowDataBound="GridView1_OnRowDataBound" OnRowDeleting="GridView1_RowDeleting" CellPadding="4" CssClass="rounded_corners" HeaderStyle-Height="40px">
<AlternatingRowStyle CssClass="rounded_corners tr tr" />
<Columns>
<asp:BoundField ControlStyle-BorderWidth="60" DataField="dateupdate" HeaderText="Date Update" DataFormatString="{0:dd/MM/yyyy}" >
<ControlStyle BorderWidth="60px" />
</asp:BoundField>
<asp:TemplateField HeaderText="Progress">
<ItemTemplate >
<%# Eval("message") %>
<br />
<asp:label ID="labelRemark" runat="server" style="Font-Size:11.5px;" text='<%# Eval("remark") %>'></asp:label><br />
<asp:Label ID="labelSelf" runat="server" style="Font-Size:11.5px;" ></asp:Label><br />
<div id="div<%# Eval("progressID") %>" >
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
DataKeyNames="progressID" style="text-align:center; font-size:small;" CellPadding="4" OnRowCommand="GridView2_RowCommand" GridLines="None" CssClass="rounded_corners" Width="500px" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="tackingNo" HeaderText="Courier Tracking No" ItemStyle-Font-Size="Small" ItemStyle-Width="150px" >
</asp:BoundField>
<asp:BoundField DataField="courierDate" HeaderText="Courier Date">
</asp:BoundField>
<asp:TemplateField HeaderText="Provider">
<ItemTemplate>
<asp:HyperLink Text='<%#Eval("providernm")%>' Target="_blank" runat="server" DataNavigateUrlFields="companyurl" NavigateUrl='<%#Eval("companyurl")%>' ></asp:HyperLink>
<br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tracking" >
<ItemTemplate>
<br />
<asp:HyperLink Text="Track Here" Target="_blank" runat="server" DataNavigateUrlFields="trackingurl" NavigateUrl='<%#Eval("trackingurl")%>' ></asp:HyperLink>
<br />
<asp:Label ID="Label4" runat="server" Text="* check after 24 hours" Font-Italic="true"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="LinkButtonDELETE" runat="server" CommandName="Delete" Text="Delete" OnClientClick= "return confirm('Confirm Delete progress?')"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Thank you.
Use GridView1_OnRowDataBound instead of foreach (GridViewRow row in GridView1.Rows). Apart from that your first approach is correct. You're simply not assigning the Text property there but reading from it.
SelfCollectNameSpace.SelfCollect address = new SelfCollectNameSpace.SelfCollect();
protected void MyGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Label labelSelf = (Label)e.Row.FindControl("labelSelf");
labelSelf.Text = address.getSelfCollectAddress(orderNoTracking); // maybe you need to retrieve the orderNoTracking from another control in this row or from it's datasource
}
}
Related
I want to display the text of a specific cell in a textbox whenever I select a row from my gridview but whenever i do this " " is the only text that I get even though the cell is not empty. Data on the gridview is bound from my database and I made a function where all the data from my database will be bound on my gridview. Below is the code that I'm using.
textbox1.Text = myGridView.SelectedRow.Cells[3].Text;
Markup
<asp:GridView ID="TraineeGrid" runat="server" AutoGenerateSelectButton ="true" AllowSorting="True" ShowHeader="true"
ShowFooter="false" AutoGenerateColumns="False" AutoGenerateEditButton="false" ScrollBars="Auto"
OnRowEditing="TraineeGrid_RowEditing"
OnRowUpdating="TraineeGrid_RowUpdating" OnRowCancelingEdit="TraineeGrid_RowCancelingEdit"
DataKeyNames="ID" Width="100%"
CellPadding="4" ForeColor="#333333" GridLines="None" HorizontalAlign="Center"
EditRowStyle-VerticalAlign="Middle" OnInit="Page_Load" OnRowDataBound="TraineeGrid_RowDataBound" OnSelectedIndexChanging="TraineeGrid_SelectedIndexChanging" OnSelectedIndexChanged="TraineeGrid_SelectedIndexChanged">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Delegates Name">
<ItemStyle HorizontalAlign="Center" Width="125px" />
<HeaderTemplate>
<asp:LinkButton ID="lbDelegate" runat="server" Text="Delegate Name" CommandName="Sort"
CommandArgument="Delegate" ForeColor="White" Font-Underline="False"></asp:LinkButton>
<br />
<asp:TextBox ID="newDelegate" TabIndex="1" runat="server"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<%# Eval("Delegate") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDelegate" runat="server"
Text='<%# Eval("Delegate") %>'/>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Rank/Position">
<ItemStyle HorizontalAlign="Center" Width="125px" />
<HeaderTemplate>
<asp:LinkButton ID="lbRankPos" runat="server" Text="Rank/Position" CommandName="Sort"
CommandArgument="RankPos" ForeColor="White" Font-Underline="False"></asp:LinkButton>
<br />
<asp:TextBox ID="newRankPos" TabIndex="2" runat="server"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<%# Eval("RankPos") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtRankPos" runat="server"
Text='<%# Eval("RankPos") %>'/>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
Function that binds Data
private void PopulateData()
{
DataTable dataTable = new DataTable();
using (SqlConnection connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["TestCS"].ConnectionString))
{
string path = "PopulateSQL.txt";
StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader(path))
{
while (sr.Peek() >= 0)
{
sb.Append(sr.ReadLine());
}
string sql = sb.ToString();
using (SqlCommand cmd = new SqlCommand(sql, connection))
{
using (SqlDataAdapter dataAdapt = new SqlDataAdapter(cmd))
{
dataAdapt.Fill(dataTable);
ViewState["NormalGrid"] = dataTable;
}
}
}
}
if (dataTable.Rows.Count > 0)
{
TraineeGrid.DataSource = dataTable;
TraineeGrid.DataBind();
}
else
{
//Displays 'No Data Found' to gridview if there are no data in table
dataTable.Rows.Add(dataTable.NewRow());
TraineeGrid.DataSource = dataTable;
TraineeGrid.DataBind();
TraineeGrid.Rows[0].Cells.Clear();
TraineeGrid.Rows[0].Cells.Add(new TableCell());
TraineeGrid.Rows[0].Cells[0].ColumnSpan = dataTable.Columns.Count;
TraineeGrid.Rows[0].Cells[0].Text = "No Data Found";
TraineeGrid.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
}
}
You can use this: textbox1.Text = Server.HtmlDecode(row.Cells[1].Text.Trim());
In OnSelectedIndexChanged :
protected void OnSelectedIndexChanged1(object sender, EventArgs e)
{
//Get the selected row
GridViewRow row = GridView1.SelectedRow;
if (row != null)
{
// With
// TextBox1.Text = (row.FindControl("lblLocalTime") as Label).Text;
// Without
TextBox1.Text = Server.HtmlDecode(row.Cells[1].Text.Trim());
}
}
Complete Markup:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
OnSelectedIndexChanged="OnSelectedIndexChanged1" AutoGenerateSelectButton="true">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="150" />
</Columns>
I've already found a solution. The problem is Im using TemplateField instead of BoundField that's why I cant use .Cells[] to get the specific cell that I wanted to get. However, if you are using TemplateField you can use .FindControl() and put in the ID of the label where your binding of data happens (ex. Text ='<%# Eval("FirstName") %>'). You can see that I put label on ItemTemplate to call it's ID.
Markup
<asp:TemplateField HeaderText="Delegates Name">
<ItemStyle HorizontalAlign="Center" Width="125px" />
<HeaderTemplate>
<asp:LinkButton ID="lbDelegate" runat="server" Text="Delegate Name" CommandName="Sort"
CommandArgument="Delegate" ForeColor="White" Font-Underline="False"></asp:LinkButton>
<br />
<asp:TextBox ID="newDelegate" TabIndex="1" runat="server"></asp:TextBox>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbName" runat="server" Text = '<%# Eval("Delegate") %>'> </asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtDelegate" runat="server"
Text='<%# Eval("Delegate") %>' />
</EditItemTemplate>
</asp:TemplateField>
Code Behind
Label varName = (Label)rows.FindControl("lbName");
string name = varName.Text;
I' ve got an issue with my program.
When i do this:
GridView gvw = this.ProduttoriList1.FindControl("GridViewList") as GridView;
SqlDataSource dataSource = this.ProduttoriList1.FindControl(gvw.DataSourceID) as SqlDataSource;
FileInfo tempXlsFile = new FileInfo(HttpRuntime.CodegenDir + "\\" + Guid.NewGuid() + ".xlsx");
string filename = string.Format("Produttori_{0}.xlsx", Guid.NewGuid().ToString());
try
{
gvw.AllowPaging = false;
gvw.DataSourceID = string.Empty;
gvw.DataSource = dataSource;
gvw.DataBind();
//...some export commands with EPPlus...
}
catch (Exception exception)
{
throw exception;
}
finally
{
System.IO.File.Delete(tempXlsFile.ToString());
}
and i'm going to read the HeaderRow, i can't read the column that has a SortExpression (return a "" value).
<asp:Panel ID="Panel2" runat="server" Style="padding-top: 10px">
<asp:GridView ID="GridViewList" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSourceList"
Width="100%" AllowPaging="True" AllowSorting="True" SkinID="GridViewDefault"
DataKeyNames="id">
<Columns>
<asp:TemplateField HeaderStyle-Width="30px" HeaderText="N°">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>.
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="DescrLongITA" HeaderText="Produttore" SortExpression="DescrLongITA" />
<asp:TemplateField HeaderText="Modifica">
<ItemTemplate>
<asp:ImageButton ID="ImageButtonEdit" runat="server" ImageUrl="~/Images/Icons/modifica.gif"
CommandArgument='<%# Eval("ID") %>' OnClick="ImageButtonEdit_Click" />
</ItemTemplate>
<HeaderStyle Width="60px" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Elimina">
<ItemTemplate>
<asp:ImageButton ID="ImageButtonDelete" runat="server" CommandName="Delete" ImageUrl="~/Images/Icons/elimina.gif"
OnClientClick="return confirm("Continuare la cancellazione?")" CommandArgument='<%# Eval("ID") %>'
OnClick="ImageButtonDelete_Click" />
</ItemTemplate>
<HeaderStyle Width="60px" />
<ItemStyle HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<div style="color: #FF0000; padding: 30px">
<asp:Label ID="LabelEmpty1" runat="server" Text="Nessun dato trovato"></asp:Label>
</div>
</EmptyDataTemplate>
</asp:GridView>
</asp:Panel>
In this case, i can't read the BoundField with HeaderText = "Produttore".
How can i do?
I found the solution, i hope to help you.
When I perform the preliminary steps of the export procedure and do this..
string[] columns = new string[gv.HeaderRow.Cells.Count];
for (int i = 0; i < columns.Length; i++)
{
columns[i] = gv.HeaderRow.Cells[i].Text;
}
Export(tmpFile, gv, columns);
i've replaced this one
columns[i] = gv.HeaderRow.Cells[i].Text;
with this one
var headertext = gv.Columns[i].HeaderText;
columns[i] = new DataColumn(headertext).ToString();
I am working in windows 8.2 and was working to insert a selected row to my data base and i have web form which have a grideview with checkboxes to select/deselect and an event to insert.
the code of the web form is as
<asp:Panel runat="server" ID="pnlStudData">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:checkbox runat="server" ID="chKCheckAll" onclick="javascript:SelectAllCheckboxes(this)"/>
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkCheck" onclick="javascript:CheckedCheckboxes(this)"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Id">
<ItemTemplate>
<asp:Label ID="lblStudName" runat="server" Text='<%#Eval("Stud_Id")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<asp:Label ID="lblFirstName" runat="server" Text='<%#Eval("FirstName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Middle Name">
<ItemTemplate>
<asp:Label ID="lblMiddleName" runat="server" Text='<%#Eval("MiddleName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<asp:Label ID="lblLastName" runat="server" Text='<%#Eval("LastName")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cumpase">
<ItemTemplate>
<asp:Label ID="lblCumpase" runat="server" Text='<%#Eval("Cumpase")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="College">
<ItemTemplate>
<asp:Label ID="lblCollege" runat="server" Text='<%#Eval("College")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Department">
<ItemTemplate>
<asp:Label ID="lblDept" runat="server" Text='<%#Eval("Dept")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Block">
<ItemTemplate>
<asp:Label ID="lblBlock" runat="server" Text='<%#Eval("Block")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dorm Number">
<ItemTemplate>
<asp:Label ID="lblDormNo" runat="server" Text='<%#Eval("Dorm")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="White" ForeColor="#000066" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#007DBB" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#00547E" />
</asp:GridView>
<br />
<asp:Button ID="Save" runat="server" Text="Save" Width="169px" Height="46px" OnClick="Save_Click" />
<br />
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
</asp:Panel>
and the java script to select/deselect a check-box as
<script language="javascript" type="text/javascript">
function SelectAllCheckboxes(chk)
{
var totalRows = $("#<%=GridView1.ClientID%> tr").length;
var selected = 0;
$('#<%=GridView1.ClientID%>').find("input:checkbox").each(function () {
if (this != chk) {
this.checked = chk.checked;
selected += 1;
}
});
}
function CheckedCheckboxes(chk)
{
if(chk.checked)
{
var totalRows = $('#<%=GridView1.ClientID %> :checkboxes').length;
var checked = $('#<%=GridView1.ClientID %> :checkbox:checked').length;
if(checked==(totalRows-1))
{
$('#<%=GridView1.ClientID %>').find("input:checkbox").each(function () {
this.checked = true;
});
}
else
{
$('#<%=GridView1.ClientID %>').find('input:checkbox:first').removeAttr('checked');
}
}
else {
$('#<%=GridView1.ClientID %>').find('input:checkbox:first').removeAttr('checked');
}
}
</script>
and the button save click event which sends this selected row cells to a database class insert method as
foreach(GridViewRow gd in GridView1.Rows)
{
if ((gd.Cells[0].FindControl("chkCheck") as CheckBox).Checked)
{
// gd.Cells[0].Visible = false;
string stId = gd.Cells[1].Text.ToString();
string fName = gd.Cells[2].Text.ToString();
string mName = gd.Cells[3].Text.ToString();
string lName = gd.Cells[4].Text.ToString();
string cumpas = gd.Cells[5].Text.ToString();
string college = gd.Cells[6].Text.ToString();
string dept = gd.Cells[7].Text.ToString();
int bl = Convert.ToInt32(gd.Cells[8].Text);
int dormNo = Convert.ToInt32(gd.Cells[9].Text);
DbCon db = new DbCon();
if (db.RegisterStud(stId, fName, mName, lName, cumpas, college, dept, bl, dormNo) == 1)
{
Label1.Text = "You Have Inserted";
}
else
{
Label1.Text = "Errror";
}
}
else { Label1.Text = "Please select a row"; }
}
but i can't find each cells value and not assigned to those string variables.
When using <asp:TemplateField>, the data is not inside Cell but is contained in the Control present inside the Cell.
row.Cells[].Text works only when using <asp:BoundField> for GridView Columns.
So, There are two steps to read the value when using <asp:TemplateField>:
Access the Control inside Cell
Access the Value from Control in step 1
I see you did the above steps to read the Checkbox value from first Column already. Follow the same for all other Columns.
if ((gd.Cells[0].FindControl("chkCheck") as CheckBox).Checked)
{
// Step 1: Access the Control inside Cell
Label lblName = (Label)gd.Cells[1].FindControl("lblStudName");
// Step 2: Access the Value from Control in step 1
string Name = lblName.Text;
// Combine Step 1 & Step 2: Access Values in one line
string Name = ((Label)gd.Cells[1].FindControl("lblStudName")).Text;
string firstName = ((Label)gd.Cells[2].FindControl("lblFirstName")).Text;
// ....same way for all Columns which use a <asp:TemplateField>
}
I have a GridView that has data bounded rows. I'm trying to get specific cell value on the SelectedIndexChanged event of DropDownList. My tries are as follows:
string temp= GridView2.SelectedRow.Cells[3].Text;
string temp = ((DataBoundLiteralControl)GridView2.Rows[0].Cells[3].Controls[0]).Text;
DataBoundLiteralControl dblc = (DataBoundLiteralControl)GridView2.Rows[0].Cells[3].Controls[0];
string temp=dblc.Text;
These all 3 of them returns null.
Moreover, the Control[0] is returning correct value of TemplateFields only, but not DataBound fields.
.aspx
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor=" #d54d7b" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#FFFAFC" AlternatingRowStyle-BackColor="#FFFFF7" AlternatingRowStyle-ForeColor="#000"
AutoGenerateColumns="false" AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging" Width="900px" Font-Names="Segoe UI Light" BorderColor="#DEDEDE">
<RowStyle HorizontalAlign="center" />
<Columns>
<asp:BoundField DataField="Description" HeaderText="Description" ItemStyle-Width="80" />
<asp:BoundField DataField="Vacancies" HeaderText="Vacancies" ItemStyle-Width="150" />
<asp:TemplateField HeaderText="Detail">
<ItemTemplate>
<asp:BoundField DataField="Date" HeaderText="Date" ItemStyle-Width="80" />
<asp:BoundField DataField="Time" HeaderText="Time" ItemStyle-Width="80" />
</Columns>
Problem you are probably facing is that, GridView1 hasn't been bounded yet.. that is the reason why Control[0] is returning correct value of TemplateFields but cells in the gridview returns null.
You can do the following
protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
/// send value to filter or bind gridView1
/// bind gridvew
string temp = GridView1.Rows[0].Cells[3].Text;
/// or any code to get values from GridView Cell
}
I Converted all <asp:BoundFields> to <asp:TemplateField> and it looked like this:
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor=" #009C0D" HeaderStyle-ForeColor="White"
RowStyle-BackColor="#FFFAFC" AlternatingRowStyle-BackColor="#FFFFF7" AlternatingRowStyle-ForeColor="#000"
AutoGenerateColumns="false" AllowPaging="true" CssClass="test"
HtmlEncode="true"
Font-Names="Segoe UI Light" BorderColor="#DEDEDE" >
<RowStyle HorizontalAlign="center" />
<Columns>
<asp:TemplateField HeaderText="Description" ControlStyle-Width="250px" ><ItemTemplate> <asp:LinkButton ID= "Description" PostBackUrl='<%# Eval("Description", "~/{0}.aspx") %>' Text='<%# Eval("Description")%>' runat="server" ></asp:LinkButton> </ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Vacancies"><ItemTemplate> <asp:Label ID="Vacancies" Text='<%# Eval("Vacancies")%>' runat="server" ></asp:Label> </ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="Detail"><ItemTemplate><asp:Label ID="City" Text='<%# Eval("City")%>' runat="server" ></asp:Label> <div style="font-size:10px"> <asp:Label ID="Label1" Text='<%# Eval("DateDay")%>' runat="server" ></asp:Label> </div> </ItemTemplate></asp:TemplateField>
//and others
</Columns>
</asp:GridView>
And get specific value by entering this code behind DropDownList:
if (dropdown.SelectedIndex != -1)
{
ListItem mySelectedItem = (from ListItem li in dropdown.Items where li.Selected == true select li).First();
foreach (GridViewRow rw in GridView2.Rows)
{
Label tv = (Label)rw.Cells[3].FindControl("City");
if (tv.Text.IndexOf(mySelectedItem.Text) != -1)
{
rw.Visible = true;
}
else
rw.Visible = false;
}
}
i have placed two gridview controls,in which i have buttons. i have linkbutton in gridview1 and button1 in gridview2.
i need to get the linkbutton id on button1 click in grdiview2.
here is the snippet of my code:
<asp:GridView ID="gvdatasubcategory" runat="server" AllowPaging="false" AllowSorting="false"
CssClass="gvdatarow" ShowHeader="false" AutoGenerateColumns="False" OnRowCommand="gvdatasubcategory_RowCommand">
<Columns>
<asp:TemplateField ItemStyle-Font-Names="Estrangelo Edessa" HeaderStyle-Font-Names="Estrangelo Edessa">
<ItemTemplate>
<div class="subcategory_type">
<div id="abd" runat="server">
<asp:LinkButton ID="lnkGridSubCategory" runat="server" CssClass='<%# "CategoryTab" + Eval("id") %>'
Width="80px" Height="26px" Text='<%#DataBinder.Eval(Container.DataItem, "SubCategory")%>'
CommandName="Test"></asp:LinkButton>
</div>
</div>
here is gridview 2:
<asp:GridView ID="Categorygvdata" runat="server" AllowPaging="false" AllowSorting="false"
CssClass="gvdatarow" ShowHeader="false" DataKeyNames="Id" AutoGenerateColumns="False"
OnSelectedIndexChanged="Categorygvdata_SelectedIndexChanged">
<HeaderStyle BackColor="#013a04" Height="25px" ForeColor="White" />
<Columns>
<asp:TemplateField ItemStyle-Font-Names="Estrangelo Edessa" HeaderStyle-Font-Names="Estrangelo Edessa">
<ItemTemplate>
<div class="category_type">
<asp:Button ID="Button1" runat="server" CommandName="FilterCategory" CommandArgument='<%# Eval("Id") %>'
CssClass='<%# "CategoryTab" + Eval("Id") %>' Text='<%# Eval("Category") %>' OnCommand="Button1_Click" />
</div>
</ItemTemplate>
<HeaderStyle Font-Names="Estrangelo Edessa" Width="5px" />
<ItemStyle Font-Names="Estrangelo Edessa" Width="5px" Wrap="false" HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
</asp:GridView>
server side code:
i have tried this but no luck.!
protected void Button1_Click(object sender, CommandEventArgs e)
{
LinkButton GridView1 = (LinkButton)gvdatasubcategory.FindControl("Categorygvdata");
foreach (GridViewRow row in gvdatasubcategory.Rows)
{
LinkButton btn = (LinkButton)row.FindControl("lnkGridSubCategory");
string strClientID = string.Empty;
strClientID = btn.ClientID;
}
}
Need help.
Thankyou.
Try this
LinkButton lnkGridSubCategory = (LinkButton)gvdatasubcategory.FindControl("lnkGridSubCategory");
foreach (GridViewRow row in gvdatasubcategory.Rows)
{
string strClientID = string.Empty;
strClientID = lnkGridSubCategory.ClientID;
}
The reason why your code is failing is that you are casting a grid view to a link button which will not work.