Auto format gridview(Asp.net) on runtime - asp.net

How can I format the gridview in asp.net on run time. Using the autoformat template?
For example, there is a gridview control, and there is dropdown list, inside the dropdownlist are the autoformat templates.
And I need to change the design of the gridview by just choosing from the dropdown list or is it even possible?

First of all, read carefuly GridView Examples for ASP.NET 2.0: Formatting the GridView. Then, on your dropdown list OnSelectedIndexChanged event, you can format which cell or column you want.
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
OnSelectedIndexChanged="DropDownList1_OnSelectedIndexChanged">
Accessing a DropDownList inside a GridView

Related

ASP.NET WebForms RequiredFieldValidator with Repeater

I have a repeater control that has textboxes and buttons for delete/update. Also in my form I have the same looking row as the repeater but this is for new records that the user can fill out. One of the fields is required so I have a RequiredFieldValidator on it. However, because they are all in the same form (because this visually looks correct), I can't update one of the records in the repeater row because the RequiredFieldValidator in the add row won't be filled out, which is fine in this case because they want to alter a different record not add a record.
So how would I be able to use the RequiredFieldValidator to require this field but only when the Add button is clicked and to ignore it if the delete or update button inside the repeater control is pressed?
Set CausesValidation on your delete and update buttons to false.
<asp:Button ID="delete" runat="server" CausesValidation="false" Text="Delete" />

ASP.NET DropDownList not getting correct SelectedItem

We are using ASP.NET for one of our projects. However, when we are trying to read the SelectedItem or SelectedValue property of the DropDownList we are using in the callback function of a Link click, we are not getting the correct SelectedItem.
<FooterTemplate>
<asp:DropDownList ID="cmbTesters" ClientIDMode="Static" runat="server" Width="300px" DataSource='<%# PopulateTesterNames() %>' DataTextField="FullName" DataValueField = "PK_ID"></asp:DropDownList>
</FooterTemplate>
This is the DropDownList in the aspx file. The drop down is present within a GridView's Footer Row. We are invoking the following set of code on clicking a link.
if (int.TryParse(((DropDownList)dgCreateCPRVerificationResponse.FooterRow.FindControl("cmbTesters")).SelectedValue, out TesterID))
{
TesterID = int.Parse(((DropDownList)dgCreateCPRVerificationResponse.FooterRow.FindControl("cmbTesters")).SelectedValue);
}
The problem we are facing is that whatever value we choose, the SelectedValue is always of the first item in the list. We are using REST based URL defined in the global.asax file. Also note that this is not built on any framework.
Please help as soon as possible
Make sure to put the binding methods of the dropdownlist and the gridview inside if (!IsPostBack)

Dynamically add items to DropDownList but still allow for a field that has no value?

I have populated my DropDownLists with different columns of items from a database but I'm trying to make it so that the first item in the list has no value with text similar to "Select an Item"
For some reason Even though I add the item to the list and make it the selected item, It gets overridden by the items from the database...
How can I accomplish this?
UPDATE:
Everything is done from the .aspx page designer but here is the generated code-
<asp:DropDownList ID="ddlUnits1" runat="server" DataSourceID="UnitsEDS"
DataTextField="unitId" DataValueField="unitId">
<asp:ListItem Selected="True">Select Units</asp:ListItem>
</asp:DropDownList>
<asp:EntityDataSource ID="UnitsEDS" runat="server"
ConnectionString="name=UnitsEntity"
DefaultContainerName="UnitsEntity" EnableFlattening="False" EntitySetName="spillunits">
</asp:EntityDataSource>
Use the ListControl.AppendDataBoundItems Property:
AppendDataBoundItems Documentation
From the documentation: "The AppendDataBoundItems property allows you to add items to the ListControl object before data binding occurs. After data binding, the items collection contains both the items from the data source and the previously added items."

Editing database with dropdown list in asp.net

I am using asp.net with vb.net code behind. I have a database that contains a list of names. I use an asp:dropdownlist databound with SqlDataSourceID and it works fine to get the list from the database to the dropdownlist and display it.
However, I want to use the dropdownlist to also allow the user to add unique values to the dropdownlist and therefore edit the database.
How do I do this? I cannot enter unique values in the dropdownlist now. Below is the asp snippet.
Thanks for the help.
<asp:DropDownList ID="OEMDropDownLst" runat="server" AutoPostBack="true"
CssClass="NwDrpDwnFmt" DataSourceID="SqlDataSource1" DataValueField="CustomerName"
AppendDataBoundItems="True">
</aso:DropDownList>
If I understand correctly you want users to type directly into your dropdownlist? As far as I know that can't be done. If you want users to add new options to that dropdownlist then you have to create the fields needed and have an update button so then you rebind the dropdownlist after changes have been saved.
</aso:DropDownList></asp:DropDownList> Spelling mistake brother.

ASP.NET Repeater template sub-control visibility before databind

I have a custom control which contains a Repeater control. The Repeater has an ItemTemplate. Inside that item template I have a panel which is going to hide something based on "IsEditable" a boolean property of the custom control. What I would like to do is set the panel's visibility once before the Repeater is databound.
I know I could do an onItemDataBound event and use FindControl to get the panel but that seems a little excessive since it will always be either visible or not for all rows and I have no other actions that need to occur on databind.
Is there a way to find the control in the ItemTemplate before the Repeater is databound?
try this:
<ItemTemplate>
<asp:Panel Visible='<%# this.IsEditable %>' runat="server">
editableStuff
</asp:Panel>
</ItemTemplate>

Resources