I have a GridView that is populated via a database, inside the GridView tags I have:
<Columns>
<asp:TemplateField>
<ItemTemplate><asp:Panel ID="bar" runat="server" /></ItemTemplate>
</TemplateField>
</Columns>
Now, I want to be able to (in the code) apply a width attribute to the "bar" panel for each row that is generated. How would I go about targeting those rows? The width attribute would be unique to each row depending on a value in the database for that row.
<asp:Panel ID="bar" runat="server" Width='<%# Eval("Width") %>' />
If you like, you can change Eval("Width") to the expression that calculates the width.
You are going to want to handle the GridView's RowCreated event. This occurs just after the GridView creates each row and will give you programmatic access to the row and all the controls contained within it.
Here is the MSDN page on the event.
I suggest you to not use Eval, if you can, because it's a little slower. In that cases I generally prefer to cast my data source to his base type:
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Panel ID="bar" runat="server" Width='<%# ((yourCustomData)Container.DataItem).Width %>' />
</ItemTemplate>
</TemplateField>
</Columns>
where yourCustomData is the row type of your data source (i.e. the element of a List<>).
This method is really faster than Eval.
Edit: oh, don't forget to include in the page a reference to the namespace that contains yourCustomData
<%# Import Namespace="yourNameSpace.Data" %>
I suggest you attach a callback handler to the OnRowDataBound event. Each row binding will fire an event which you can handle in your callback handler.
In that callback handler, you can get the info about the item being bound to the row and apply the width according to the values in your database.
Related
I have a gridview that is serviced by a sqldatasource and I was just wondering how is it possible to have the Edit button do something other than put the gridview row in edit mode.
<asp:CommandField ShowSelectButton="True" ShowEditButton="true" ButtonType="Button" />
What I want to do is have a custom edit page/control that is loaded when you click Edit.
As opposed to just put the row in edit mode and turn the fields into editable textboxes.
Is something like that possible?
There are two options that come to my mind:
Use EditTemplates for your GridView fields
This will let you customize the look / content of each of the fields when they go into edit mode.
<Columns>
<asp:TemplateField ...>
<EditItemTemplate>
<!-- child controls -->
</EditItemTemplate>
<ItemTemplate>
<!-- child controls -->
</ItemTemplate>
</asp:TemplateField>
</Columns>
Handle the RowEditing event
You can handle the RowEditing event to do whatever manipulation on the Gridview that you want in repsonse to a "Edit" button in the row being clicked.
I have a requirement to display row number in grid view. What is the best way to display the row number using BoundField or TemplateField?
Note: This need to be done using markup only (without code behind).
Note: When sorting happens, the row number should not be in sequence, the first row should go down with its content.
I have already referred the following:
http://social.msdn.microsoft.com/forums/en-US/winformsdatacontrols/thread/2eead3e3-5cc2-40f7-a91c-8f7942d5329c/
<asp:TemplateField HeaderText="#" >
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
By the way, this solution proposed in article you referred. Why you don't like it and ask here?
The best place to do this would be to use the templatefield
<asp:TemplateField HeaderText="Row Number">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
An examples http://www.devcurry.com/2010/01/add-row-number-to-gridview.html
The BoundField displays the value of specified DataSource field as text.
The TemplateField allows to mix html or make use of web controls.
Please refer to the following explanation to confirm the difference.
http://forums.asp.net/t/1804988.aspx/1
The gridview is rendered as html table. If you don't want to calculate the row number in code behind, you should use JQuery.
var rowCount = $('#myTable tr').length;
And you should fill the table footer with rowCount value.
using Visual.Web.Developer.2010.Express;
using SQL.Server.Management.Studio.2008.R2;
N00b here,
I've got the gridview to look the way I want it to (a textbox inside of the ItemTemplate). The Textbox's class has some client-sideJS that enables a save button (an asp:LinkButton set to look like a Jquery UI save icon) to become visible after the Textbox's .keypress event fires..
Now for my question..I've looked everywhere, but I can't get how to have gridview put the Sql server db content in that textbox on Page_load (one textbox + <br /> for each row). I'm only printing one collumn from the Sql server db into the Gridview.. Also, how would I bind the asp:LinkButton save button to gridview's save event? If there is a more effecient way to do this? If you have some insight for me, please give me your opinion/!
My .aspx code
<asp:TemplateField >
<ItemTemplate>
<asp:TextBox ID="TextBox1" class="hexen" runat="server" DataField="TbValue" SortExpression="TbValue">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:FluxConnectionString %>"
SelectCommand="SELECT [TbValue] FROM [InvestigateValues]">
</asp:SqlDataSource>
Thanks in advance!
Change your text box to
<asp:TextBox ID="TextBox1" class="hexen" runat="server" text='<%#Bind("TbValue")%>' />
This will enable two way databinding.
Here is an article to get you started: http://www.devx.com/DevX/Article/35058.
The grid view and SqlDataSource expose Insert, Update and Delete event/methods. These are on a row level, not grid level.
The way I would approach your problem would be to have an onclick event for your link button that iterates through the gridview, get the data from each text box and then perform the appropriate data base action in the code behind.
I have GridView with my DAO as data source rendering some person data (name, surname...). I want to add to rendered table simple link to expanded view with more informations. Ithought I could add html link with POST argument to each row. But every html I try to pass to GridView gets escaped. Can I somehow unescape it? Or is there any other simple way?
It is my private, very quick project, I donĀ“t need any robust solution. Just the simplest and quickest one. Thanks.
It sounds like you want to use an ItemTemplate in your gridview to render HTML controls within the grid:
asp:GridView ID="myGrid" runat="server">
<Columns>
<asp:TemplateField HeaderText="Last Name">
<ItemTemplate>
<!--Any HTML can go here, below is a label -->
<label><%# DataBinder.Eval (Container.DataItem, "lastname") %><label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
How do you call a codebehind/class method from a gridview in an aspx page? Also, I need to pass the value of databound column to that method. Is this possible?
Something like this:
<asp:BoundField DataField="Precision" />
<asp:BoundField DataField="MyNumber" DataFormatString="FormatHelper.Format(MyNumber, Precision)" />
Edit: I tested this to work.
<asp:TemplateField HeaderText="My Number">
<ItemTemplate>
<asp:Label runat="server"
Text='<%# Eval("MyNumber", FormatHelper.Format((decimal)Eval("MyNumber"), (decimal)Eval("Precision") )) %>' />
</ItemTemplate>
</asp:TemplateField>
This works because the Eval method takes the DataFormatString as an optional second parameter. note: Make sure you replace the casts with the appropriate types for the method signature.
An easier way might be to bind to a linq-to-objects projection instead.
myGridView.DataBind( from foo in fooList
select new {
MyNumber = MyNumber,
Precision = Precision,
MyFormattedNumber = FormatHelper.Format(MyNumber, Precision),
} );
GridView.DataKeys Property
Button.CommandArgument Property
Create a template field in the gridview.
Add a button to the template field and give the button a
commandName (example: "btn")
Handle the rowdatabound event and check if rowType=DataRow then:
Get the button within this row using the FindControl method then
fill in its CommandArgument the value you want(example: e.Row.DataItem("ID"))
Handle the rowcommand event:
if commandname="btn" then
the button is clicked