Troubles retrieving data from within OnRowCommand from a GridViews - c#

If I have this <asp:ButtonField runat="server" DataTextField="Name" CommandName="GetName"></asp:ButonField> Within a GridView. Is there any way to retrieve the DataTextField (Name) from the OnRowCommand method?
<asp:GridView ID="GridView1" runat="server"
AllowPaging="True" AutoGenerateColumns="False"
DataSourceID="ObjectDataSource_Names"
DataKeyNames="ID,ModuleId" OnRowCommand="ChangeName">
Or alternatively, is there any way to make CommandName into an attribute where the command is dynamically entered based on given data, similar to the difference between DataTextField vs TextField.

I added <asp:BoundField DataField="Name" /> Then you can retrieve it using the same method here: GridView.onRowCommand Method
All I need to do now is find out a way to hide the field. If anyone knows how to, please let me know. Visible="false" gets rid of the field all together...

Not really sure if this is the best way, but it works:
Make your column a templatefield and bind the name value to a asp:hiddenfield control:
<asp:TemplateField>
<ItemTemplate>
<asp:HiddenField ID="hfName" runat="server" Value='<%# Eval("Name") %>'>
</asp:HiddenField>
</ItemTemplate>
</asp:TemplateField>

Related

ASP.NET Grid view Data filed Column Formatting Not working

I have a problem with the GridView column formatting.
I have a PhoneNumber that is currently coming from database as (###)###-#####
but I want the format to be ############ without spacing and brackets. I have tried every thing like DataFormatString="{0:###-####}" or than converting it into TemplateField and giving it format but not working.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField HeaderText="Phone Number">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#
Bind("PhoneNumber") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server"
Text='<%# Bind("PhoneNumber", "{0:d}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
This is before converting the column to TemplateField:
<asp:BoundField DataField="PhoneNumber" HeaderText="Phone Number Home"
SortExpression="Phone" DataFormatString="{0:######-####}" HtmlEncode="false" />
But nothing is working. I still get the format of phone number like this (123) 123-4567.
If you are dynamically binding the rows in code behind, you should be formatting the columns in the DataBind event of the gridview.
The semantical column formatting works only when you use bind data semantically, for example declare
Try to replace Text property in TemplateField:
Text='<%# Bind("PhoneNumber", "{0:d}") %>'
with:
Text='<%# String.Format("{0:##########}",
Convert.ToInt64(DataBinder.Eval(Container.DataItem, "PhoneNumber")))%>'
Use "#" Custom Specifier.
Formatting is solely additive, not subtractive. To accomplish this, you would need to expose a version of the values which has non-numerics stripped out, so that your client-side formatting can act upon it.

Accessing Header Text property of GridView TemplateField in the Code Behind when it is set from Header Template

I have a GridView in an asp .net application, where the Header Text of a template field is set in the Header Template of the field, as a label (where it will come from the resource file). Below is the code
<asp:GridView ID="gridView" ClientIDMode="Static" runat="server" AutoGenerateColumns="False" meta:resourcekey="grdViewResource">
<Columns>
<asp:TemplateField meta:resourcekey="TemplateFieldResource1">
<HeaderTemplate>
<asp:Label ID="lblNameHeader" Text="Name" runat="server" meta:resourcekey="lblNameHeaderResource1"/>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lblNameValue" Text='<%# Eval("Name") %>'/>
</ItemTemplate>
</Columns>
</asp:GridView>
In code behind I am trying to access the HeaderText set on the column like this
var headerText = gridView.Columns[0].HeaderText;
But the value is coming Empty and I am not able to retrieve it from the gridView.Columns' HeaderTemplate property as well.
Please help me.
You have several problems with your code.
You are missing a closing </asp:TemplateField>. Your asp:Label is missing a runat="server" attribute.
If you want to use the .HeaderText property, this should be your markup:
<asp:GridView ID="gridView" ClientIDMode="Static" runat="server" AutoGenerateColumns="False" meta:resourcekey="grdViewResource">
<Columns>
<asp:TemplateField meta:resourcekey="TemplateFieldResource1" HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblNameValue" Text='<%# Eval("Name") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
If you want to use a HeaderTemplate with custom markup in it, then you need to cast the column to a TemplateField, then access the controls within it.

reverse asp.net grid view columns header into rows header

I'm still learning asp.net and I want to ask you about how to reverse the grid view, I mean the default grid view would be like this
and this is the code for it:
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True"
AutoGenerateColumns="false" Width="256px">
<Columns>
<asp:TemplateField HeaderText="1"> <ItemTemplate>
<asp:Label ID="test1" runat="server" Text="test1"></asp:Label>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="2"> <ItemTemplate>
<asp:Label ID="test2" runat="server" Text="test2"></asp:Label>
</ItemTemplate></asp:TemplateField>
<asp:TemplateField HeaderText="3"> <ItemTemplate>
<asp:Label ID="test3" runat="server" Text="test3"></asp:Label>
</ItemTemplate></asp:TemplateField> </Columns>
<RowStyle HorizontalAlign="Left" VerticalAlign="Bottom" />
</asp:GridView>
and i want it to be like this picture :
the test here will be data bound and the numbers should be header text, this is only for test, is there a way to reverse it?
thank you all for your valuable advises and efforts, I really appreciate it.
I was able to achieve what I want by pivoting the data table and the columns became rows and the opposite.
this link can explain it.
thx

Repeater inside other control

I'm using Telerik's AJAX controls, and I have a slight issue. In my application the user enters the number of 'weeks', and then the program dynamically adds RadPane's to a repeater (with a splitter so they can modify the size), with RadGrid's inside the panes.
I have the following markup:
<telerik:RadSplitter ID="splt" runat="server" Width="100%">
<asp:Repeater ID="rptSplitter" runat="server" OnItemDataBound="rptSplitter_ItemDataBound">
<ItemTemplate>
<telerik:RadPane ID="pane" runat="server">
<asp:Label ID="lblText" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"From") %>'></asp:Label>
<telerik:RadGrid ID="gv" runat="server" AutoGenerateColumns="false">
<MasterTableView runat="server" DataKeyNames="ID">
<Columns>
<telerik:GridBoundColumn DataField="ConsignmentNo" HeaderText="Consignment" SortExpression="ConsignmentNo">
</telerik:GridBoundColumn>
<telerik:GridBoundColumn DataField="Customer" HeaderText="Customer" SortExpression="Customer"></telerik:GridBoundColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
</telerik:RadPane>
</ItemTemplate>
<SeparatorTemplate>
<telerik:RadSplitBar runat="server"></telerik:RadSplitBar>
</SeparatorTemplate>
</asp:Repeater>
</telerik:RadSplitter>
When binding the repeater however, I get the following exception (well, the exception doesn't display, but I can 'see' it in the itemdatabound event of the grid):
"RadPane must be placed inside a RadSplitter control."
Any ideas on how I can somehow get this to work?
I imagine you could get around this by putting everything inside the repeater control into a usercontrol instead. Ditch the repeater control, then loop through your collection, dynamically adding the panes with the usercontrols in them.
Telerik has a demo of dynamically adding panes to a splitter.

Gridview disable edit on 1 column asp.net

I am using a gridview Edit to edit the values i have in my gridview, when i press edit, all columns can be edited, i would like that one of the columns is not allowed to be edited.
Is there any way i can do this?
Thiss is my aspx code:
<asp:GridView
ID="GridView1"
runat="server"
AllowSorting="True"
OnRowCommand="GridView1_SelectedIndexChanged1"
AutoGenerateEditButton="True"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
CellSpacing="10"
OnRowUpdating="GridView1_RowUpdating" ShowFooter="True"
onselectedindexchanged="GridView1_SelectedIndexChanged"
OnRowEditing="GridView1_RowEditing">
</asp:GridView>
Thanks
If you are using asp:BoundField, try
<asp:BoundField DataField="CustomerID"
ReadOnly="true"
HeaderText="Customer ID"/>
Or else if you are using asp:TemplateField, you can either
Render it in either an asp:Label inside EditItemTemplate
Omit the EditItemTemplate for that column altogether
Sure, make use of the EditItemTemplate. In the following example field ID will not be edited in the Edit mode:
<asp:GridView runat="server">
<Columns>
...
<asp:TemplateField HeaderText="ID">
<EditItemTemplate>
<%# Eval("ID") %>
</EditItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
please show some markup. Quick and dirty I think depending on your markup aspx you can remove the textbox from the EditItem template for the column you want to prevent editing... there are also other solutions of course :)
If you are using template field
((TemplateField)gvGridView.Columns[index]).EditItemTemplate = null;
if boundfield
((BoundField)gvGridView.Columns[index]).ReadOnly = true;

Categories

Resources