Get value of gridview.selected.row[] with visible property = 'false' - asp.net

Hi from this field in my gridview, I'd like to pass the id value when the select command field is clicked, but i don't want to have the id field visible so I have the visible property set to false; however, when I pass it on the SelectedIndexChanged event the value is "" yet when the visible property is set to "true" the text value passes fine. What is the correct way to do this?
<asp:BoundField DataField="Project_ID" Visible="false"/>
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
String ProjID = GridView1.SelectedRow.Cells[10].Text;
}

Try this:
.aspx
<asp:Gridview id="GridView1" runat="server" DataKeyNames="Project_ID" />
.cs
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
if(gridView.SelectedDataKey != null)
{
var selectedId = GridView1.SelectedDataKey.Value;
}
}
Here you'll find more info about DataKeys in Gridviews: http://msdn.microsoft.com/de-de/library/system.web.ui.webcontrols.gridview.selecteddatakey(v=vs.110).aspx

I have done something like this in winform maybe can help you. That is what i used
int rowindex = dataGridView1.CurrentRow.Index;
string ProjID= dataGridView1.Rows[rowindex].Cells[10].Value.ToString();

you could use HiddenField inside your gridView ItemTemplate to keep the ID and use it in onrowcommand event, like below:
.aspx
<asp:GridView ID="gridProject" runat="server"
onrowcommand="gridProject_RowCommand">
<Columns>
<ItemTemplate>
<asp:HiddenField ID="hidProjectID" runat="server"
Value='<%# ((DataRowView)Container.DataItem)["Project_ID"] %>' />
<asp:Button ID="btnProject" runat="server" Text="use pro id"
CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"
CommandName="DoSomething"></asp:Button>
</ItemTemplate>
</Columns>
</asp:GridView>
aspx.cs
protected void gridProject_RowCommand(object sender, GridViewCommandEventArgs e)
{
int index = 0;
GridViewRow gridRow;
GridView grid = sender as GridView;
try
{
switch (e.CommandName)
{
case "DoSomething":
index = Convert.ToInt32(e.CommandArgument);
row= gridProject.Rows[index];
string Id = ((HiddenField)row.FindControl("hidProjectID")).Value;
//do whatever you want here
break;
// and you can have as many commands as you want here
}
}
catch { //display error }
}

Related

Telerik RadGrid not showing any data in ASP.NET

I'm trying to make a really simple grid for some data on an ASP.NET page, but clearly I'm doing something wrong here. First of all, let me show you the grid I have on my clientside:
<asp:Panel runat="server" ID="pnlDoorAccess" Visible="False">
<asp:UpdatePanel runat="server" ID="upnlDoorAccess" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<tel:RadGrid runat="server" ID="gvDoorAccess" AllowSorting="false" AllowPaging="false"
CssClass="col-sm-12 noPadding" MasterTableView-CssClass="table table-hover table-header-bg table-striped no-footer tableHeaderBorder"
OnNeedDataSource="radDoorAccess_NeedDataSource" OnItemDataBound="radDoorAccess_ItemDataBound">
<MasterTableView AutoGenerateColumns="false" TableLayout="Fixed" Caption="" FilterExpression="" AllowNaturalSort="false" DataKeyNames="Month" NoMasterRecordsText="No records to Display">
<Columns>
<tel:GridBoundColumn DataField="DoorName" HeaderText="Door Name" UniqueName="DoorName"></tel:GridBoundColumn>
</Columns>
</MasterTableView>
</tel:RadGrid>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
So I wanna test the grid by only showing 1 column (DoorName), but so far it's not showing anything. Next is the server side code:
The DoorAccess property will fire up Controller.GetDoorAccess(CurrentUser.Id) when the Object Memorystore is empty and return me a DataTable object which I will return and eventually store into a DataSource property later on.
protected List<DoorAccess> DoorAccess
{
get
{
if (omsDoorAccess.DataItem == null || omsDoorAccess.DataItem.GetType() != typeof(List<Option>)) omsDoorAccess.DataItem = Controller.GetDoorAccess(CurrentUser.Id);
return (omsDoorAccess.DataItem as List<DoorAccess>);
}
set
{
omsDoorAccess.DataItem = value;
}
}
protected void Page_Load(Object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadTab_DoorAccess();
}
}
private void LoadTab_DoorAccess()
{
// Future implementation
ReloadTab_DoorAccess();
}
private void ReloadTab_DoorAccess()
{
gvDoorAccess.DataBind();
upnlDoorAccess.DataBind();
upnlDoorAccess.Update();
}
protected void radDoorAccess_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
(sender as RadGrid).DataSource = DoorAccess;
// DoorAccess holds 128 items
}
protected void radDoorAccess_ItemDataBound(object sender, GridItemEventArgs e)
{
if (e.Item.ItemType != GridItemType.Item && e.Item.ItemType != GridItemType.AlternatingItem)
{
// Do stuff in the future
}
}
So what exactly is going on here? There is probably something obvious I'm missing, but right now I don't see it.
If this question makes no sense, could you show me a block of code that would help me create a simple grid?
I see that you have AutoGeneratedColumns="False", a single column declared with the DataField="DoorName", and DataKeyName="Month".
When using Advanced DataBinding the datasource you are binding to must match the schema in the RadGrid unless you are using AutoGenerateColumns.
Without seeing your controller code, I think the issue is with the above Markup. There is no way for the grid to bind the single column with the DataField of "DoorName" to the DoorAccess object using the DataKeyName of "Month"(not knowing where "Month" is coming from). Make sure the DataKeyName property contains a unique value from the DoorAccess object and that the column DataField is equal to any property in the DoorAccess object.
ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" OnNeedDataSource="RadGrid1_NeedDataSource">
<MasterTableView AutoGenerateColumns="False" DataKeyNames="Id" CommandItemDisplay="Top">
<Columns>
<telerik:GridBoundColumn DataField="Id" UniqueName="MyId" HeaderText="My Id"></telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Value" UniqueName="Value" HeaderText="My Value"></telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
C#:
public class MyDataModel
{
public int Id { get; set; }
public string Value { get; set; }
}
protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
List<MyDataModel> datasource = new List<MyDataModel>();
for (i = 0; i <= 10; i++) {
datasource.Add(new MyDataModel {
Id = i,
Value = "Value" + i.ToString
});
}
((RadGrid)sender).DataSource = datasource;
}
I would first try with autogenerated columns on to make sure valid data is being returned from the NeedDataSource event, and then make sure your DataKeyNames and DataFields are valid properties. Additionally as #Seano666 said, because your grid is in an UpdatePanel any errors the RadGrid throws due to incorrect formatting will be suppressed.

How to retrieve textbox value in gridview when happen indexchanging event in asp.net?

I have a GridView that is bound to an ObjectDataSource. I 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 and return null refrence when run this page.
What should I do? please help..
Here's the ASPX file and the code behind:
<asp:TemplateField HeaderText="content" ItemStyle-Width="150">
<ItemTemplate>
<asp:TextBox ID="Name" runat="server" />
</ItemTemplate>
<ItemStyle Width="150px"></ItemStyle>
</asp:TemplateField>
<asp:ButtonField CommandName="Select" Text="save" ButtonType="Button" />
Code-Behind:
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
string s = row.Cells[0].Text;
//Fetch value of Name.
TextBox tb = (TextBox)GridView1.FindControl("Name");
string name = tb.Text;
}
value of name is null...
GridViewRow also has FindControl method. You may try,
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
TextBox tb = row.FindControl("Name") as TextBox;
String name = tb.Text;
}
you can do it in the row command event also .try this..
add OnRowCommand="gvProducts_RowCommand"
then in code behind
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Select"))
{
GridViewRow row = GridView1.Rows[e.RowIndex];
TextBox tb = (TextBox)row.FindControl("Name");
string name = tb.Text;
}
}

asp net repeater checkbox

I use a Checkbox in a Repeater, how can I know which
Checkbox have changed in OnCheckedChanged?
I have tried to set id then checkbox is binding data, but
it will not work. Hope someone can help me
Thanks
/Mats
Check the sender(Event Target) parameter
protected void Chb_Changed(object sender, EventArgs e)
{
if (sender != null)
{
CheckBox cb=(CheckBox)sender;
string clickedCheckBoxID=cb.ID;
}
}
Try following . Please note that we can also bind some primary column let's say "ID" column in some hidden field then get in code behind.
ASPX Side
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="sqldtasource" >
<ItemTemplate>
<asp:CheckBox ID="chk" runat="server" AutoPostBack="true" Text='<%#Bind("Name")%>' OnCheckedChanged="Chb_Changed"/>
<asp:HiddenField ID="hdn_ID" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "ID") %>'/>
</ItemTemplate>
</asp:Repeater>
Code Behind :
protected void Chb_Changed(object sender, EventArgs e)
{
if (sender != null)
{
try
{
var hdnID = (HiddenField)checkBox.NamingContainer .FindControl("hf_ID");
if(hdnID != null)
{
string primaryFieldValue = hdnID.Value;
}
if (((CheckBox)sender).Checked)
{
Response.Write(((CheckBox)sender).Text + " is checked");
}
}
catch {
}
}
}

How to make visible a column of gridview?

I am using Gridview.In this i have 2 columns i.e. department and emailID.In this gridview department is bind from database and showed in linkbutton in gridview.
I want to do that when i clicked on thst dept linkbutton it make visible the column emailID.
How can i do this?Plaese guide me..
Thanks in advance.
Here is My Grid:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkbtnDept" runat="server" Text='<%#Bind("Department")%>' OnClick="lnkbtnTitle_Click" ></asp:LinkButton>
</ItemTemplate>
<ItemStyle HorizontalAlign="Left" Width="50%" />
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Panel ID="pnlN24" runat="server" Visible="false">
<asp:Label ID="lblTotal" runat="server" Width="30" Text="abc"></asp:Label>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
You have to handle the events of GridView control especially RowCommand.
public class Demo
{
public string Dept { get; set; }
public string Email { get; set; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
List<Demo> list = new List<Demo>()
{
new Demo() { Dept="A", Email="a#a.com" },
new Demo() { Dept="B", Email="b#b.com" },
};
GridView1.DataSource = list;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "cmd")
{
GridViewRow row = (e.CommandSource as LinkButton).NamingContainer as GridViewRow;
Label email = row.Cells[1].FindControl("email") as Label;
email.Visible = true;
}
}
Understanding that you will make visible all the cells in the column for all the rows, you can handle the OnClick event of the dept button. For example.
<asp:LinkButton ID="lnkDept" OnClick="LinkButton_Click" runat="server" Text="Department" />
Code behind:
protected void LinkButton_Click(Object sender, EventArgs e)
{
gridView1.Columns[1].Visible=true;
}
Where gridView.Column[1] is your email column.
its not possible to make cell visible when column's visibility property set to false. so for showing only adjacent cell visible .
you can use the only one column of type template field and then make a table in that template field and set the td of email to visible false and on row command of grid as the previous answers making that lable visible you should make your td visible .
protected void lnkbtnTitle_Click(object sender, EventArgs e)
{
GridViewRow gvrow = ((LinkButton)sender).Parent.Parent as GridViewRow;
Panel pnlN24 = (Panel)gvrow.FindControl("pnlN24");
pnlN24.Visible = true;
}
It is very simple , the above code will work.
We have to find in which row the LinkButton is Clicked , which we can get from the
the following code. Once you get the GridViewRow then find the control in that row and make it Visible
GridViewRow gvrow = ((LinkButton)sender).Parent.Parent as GridViewRow;
As we know that
Control <--- Cell <-- GridViewRow <-- GridView
For Eg:
LinkButton <--- Cell <-- GridViewRow <-- GridView
GridViewRow is Parent to Cell and Cell is Parent to the Control inside the Cell (Panel)
function validateColors(id) {
var grid = document.getElementById("GridView1");
var label = grid.rows[id].cells[6].children[2];
grid.rows[id].cells[6].children[2].style.visibility = "visible";
grid.rows[id].cells[6].children[2].style.visibility = "hidden";
}

Why is the footer-item not included in Repeater.Items?

I need to get a value from a textbox inside a FooterTemplate in the OnClick event of a button. My first thought was to loop through the items-property on my repeater, but as you can see in this sample, it only includes the actual databound items, not the footer-item.
ASPX:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
Item<br />
</ItemTemplate>
<FooterTemplate>
Footer<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:Repeater>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
Code-behind.cs:
protected void Page_Load(object sender, EventArgs e)
{
ListItemCollection items = new ListItemCollection();
items.Add("value1");
items.Add("value2");
Repeater1.DataSource = items;
Repeater1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Debug.WriteLine(Repeater1.Items.Count);
}
This code will only output "2" as the count, so how do I get to reference my textbox inside the footertemplate?
From the MSDN documentation, the Items is simply a set of RepeaterItems based off the DataSource that you are binding to and does not include items in the Header or FooterTemplates.
If you want to reference the textbox, you can get a reference on ItemDataBound event from the repeater where you can test for the footer.
E.g.
private void Repeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
TextBox textBox = e.Item.FindControl("TextBox1") as TextBox;
}
}
You can find controls in the repeater. That will give you all the controls in the repeater (RepeaterItems collection). Now you can do something like this:
RepeaterItem footerItem=null;
foreach(Control cnt in Repeater1.Controls)
{
if(cnt.GetType() == typeof(RepeaterItem) && ((RepeaterItem)cnt).ItemType == ListItemType.Footer)
{
footerItem = cnt;
break;
}
}
The footer should be the last child control of the repeater so you can do something like..
RepeaterItem riFooter = Repeater1.Controls[Repeater1.Controls.Count - 1] as RepeaterItem;
if (riFooter != null && riFooter.ItemType == ListItemType.Footer) {
TextBox TextBox1 = riFooter.FindControl("TextBox1") as TextBox;
if (TextBox1 != null) {
TextBox1.Text = "Test";
}
}

Resources