I have reviewed several answers that all seem to indicate that using the format of [textboxID].Text should allow me to reference the input data when I am in the code behind, but this does not hold true for me. My example is as follows:
/** .aspx markup file **/
'''
<asp:ListView ID="ListViewWorkTypes" runat="server" InsertItemPosition="LastItem" >
<LayoutTemplate>
<table id="TypeRecords" runat="server" >
<tr id="TR1" runat="server" >
<td id="Td1" runat="server" >
<table runat="server" id="TypePlaceholder" >
<tr id="Tr2" runat="server" >
<td id="Td10" hidden="hidden" runat="server">Task Name</td>
<td id="Td20" runat="server">PhaseId</td>
<td id="Td30" runat="server">DeptId</td>
<td id="Td11" runat="server">Initial Value</td>
<td id="Td21" runat="server">Order</td>
<td id="Td23" runat="server">Math Operator</td>
<td id="Td31" runat="server">Option Value 1</td>
<td id="Td41" runat="server">Option Value 2</td>
<td id="Td51" runat="server">Option Value 3</td>
<td id="Td61" runat="server">Option Value 4</td>
<td id="Td71" runat="server">Option Value 5</td>
</tr>
<tr runat="server" id="itemPlaceholder">
</tr>
<tr runat="server">
<td runat="server" colspan="2">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Link" />
<asp:NumericPagerField />
<asp:NextPreviousPagerField ButtonType="Link" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</td>
</tr>
</table>
</LayoutTemplate>
<InsertItemTemplate>
<tr id="NewWorkTaskRow">
<td id="NewTaskName">
<asp:Label runat="server" ID="TaskNameLabel" Visible="true" AssociatedControlID="TaskNameTextBox" Text="Task Name"/>
<asp:TextBox ID="TaskNameTextBox" runat="server" Visible="true" Text='<%# Bind("Name") %>' /><br />
</td>
<td id="NewPhase">
<asp:Label runat="server" ID="PhaseLabel" Text="Phase" AssociatedControlID="PhaseDropDown"/>
<asp:DropDownList ID="PhaseDropDown" runat="server" DataSourceID="SqlDataPhase" DataTextField="PhaseName" DataValueField="PhaseID" AppendDataBoundItems="true" >
<asp:ListItem Text="Choose Phase" Value="" Selected="False"></asp:ListItem>
</asp:DropDownList>
</td>
<td id="NewFunctionalArea">
<asp:Label runat="server" ID="DeptLabel" Text="Fuctional Area" AssociatedControlID="DeptDropDown" />
<asp:DropDownList ID="DeptDropDown" runat="server" DataSourceID="SqlDataDept" DataTextField="Dept" DataValueField="DeptId" AppendDataBoundItems="True">
<asp:ListItem Text="Choose Functional Area" Value="" Selected="True"></asp:ListItem>
</asp:DropDownList>
</td>
<td>
<asp:Label runat="server" ID="BaseValueLabel" AssociatedControlID="BaseValueTextBox" Text="Base Value" />
<asp:TextBox ID="BaseValueTextBox" runat="server" Text='<%# Bind("BaseValue") %>' /><br />
</td>
<td>
<asp:Label runat="server" ID="OrderLabel" AssociatedControlID="OrderTextBox" Text="Order" />
<asp:TextBox ID="OrderTextBox" runat="server" Text='<%# Bind("ordr") %>' /><br />
</td>
<td>
<asp:Label runat="server" ID="MathOperatorLabel" AssociatedControlID="MathOperatorTextBox" Text="Math Operator" />
<asp:TextBox ID="MathOperatorTextBox" runat="server" Text='<%# Bind("MathOperator") %>' /><br />
</td>
<td>
<asp:Label runat="server" ID="Opt1Label" AssociatedControlID="Opt1TextBox" Text="Option Value" />
<asp:TextBox ID="Opt1TextBox" runat="server" Text='<%# Bind("Opt1") %>' /><br />
</td>
<td>
<asp:Label runat="server" ID="Opt2Label" AssociatedControlID="Opt2TextBox" Text="Option Value" />
<asp:TextBox ID="Opt2TextBox" runat="server" Text='<%# Bind("Opt2") %>' /><br />
</td>
<td>
<asp:Label runat="server" ID="Opt3Label" AssociatedControlID="Opt3TextBox" Text="Option Value" />
<asp:TextBox ID="Opt3TextBox" runat="server" Text='<%# Bind("Opt3") %>' /><br />
</td>
<td>
<asp:Label runat="server" ID="Opt4Label" AssociatedControlID="Opt4TextBox" Text="Option Value" />
<asp:TextBox ID="Opt4TextBox" runat="server" Text='<%# Bind("Opt4") %>' /><br />
</td>
<td>
<asp:Label runat="server" ID="Opt5Label" AssociatedControlID="Opt5TextBox" Text="Option Value" />
<asp:TextBox ID="Opt5TextBox" runat="server" Text='<%# Bind("Opt5") %>' />
</td>
<td>
<asp:LinkButton ID="InsertButton" runat="server" OnClick="InsertType" Text="Insert" ></asp:LinkButton>
</td>
</tr>
</InsertItemTemplate>
'''
/** Code Behind **/
'''
protected void InsertType(object sender, EventArgs e)
{
string Name = "";
int BaseValue = 0;
bool LOE;
int ordr;
char operation;
int Opt1 = 0;
int Opt2 = 0;
int Opt3 = 0;
int Opt4 = 0;
int Opt5 = 0;
int TypeID = 0;
string strtypecon = WebConfigurationManager.ConnectionStrings["DefaultEstimateConnection"].ConnectionString;
SqlConnection typeconx = new SqlConnection(strtypecon);
SqlCommand TypeCmd = new SqlCommand()
{
Connection = typeconx
};
/*** Attempted with the aforementioned TextBox ID reference ***/
Name = TaskNameTextBox.Text;
/*** Err msg: TaskNameTextBox does not exist in current context ***/
/*** Attempted with Request form ***/
Name = Request.Form["TaskNameTextBox"];
/** Checkpoint at this point of the process indicates a null value for Name **/
ListViewWorkTypes.FindControl("TaskNameTextBox").ToString();
TypeCmd.Parameters.AddWithValue("#Name", Name);
'''
This did not seem different from the other code I had seen on this site that had reportedly worked.
Ok,a few things:
first, if you not using the grids built in edit or features, then I would dump all templates EXCEPT for the ItemTemplate. I find in most cases, I don't need all the additional templates, and I blow them out (delete them).
So, I will often drop in a lv, use the wizard to generate the data and layout. I then delete the datasource on the page and then collapse each edit/alternative/insert/edit templates and DELETE them. blow out (delete) the datasource control placed on your form, and you go from a galactic mess of huge markup into a simple nice clear easy to work with lv.
So, even in your case, it not clear if you actually using the insert template, or that you simply drop in a linkbutton and added a event to that link button.
(BTW: you can drop in a plane jane asp.net button - they often look MUCH nicer).
So, I don't know if you using the insert template, but you do have the button dropped into your insert template, but I don't know if its ALSO in the item template.
As noted:
You have (or might) have item/edit/alternating/insert templates. And given the screen might also have many rows? The when you reference a text box, then which one and which row and from which template would you get?
Answer: massive world wide confusing!!!
So, when using a gridview, listview, repeater, formview, details view?
They all work quite much the same - they are "repeaters" in the sense that the layout you have is used over and over. As a result, you of course cannot use ay:
TaskNameTextBox.Text
Now, as noted, it not at all clear if you actually using the inert template. and as noted, in most cases, I simple dump ALL the templates except for Item template. And in most cases it is less code, less markup, less things to maintain.
Ok, however lets address the linkbutton click event and code.
There are actually about 5 approaches here. and picking which approach to get the row you trying to work on will DEPEND VERY much on what you trying to do.
Probably the most easy way to get this to work?
You can add to the button a CommandName tag. This tag has specials names that can cause the iistview events to fire. However, looking at your listview, it don't look like you have ANY of the wired up.
So, you could use CommandName="select"
This will then cause the row command to fire (but row index has NOT changed yet!!! - it will. However, RowCommand event does pass the row.
but, right after above, the SelectedIndex of the listview will fire and MORE important the index DOES change.
However, there also another way to get the current row, and save you to having to learn the COMPLEX event model of the listview.
What you can do is simply pick up and get the current row you clicked on (the insert link button you have). When we get that row you can THEN pluck out values from that row.
However, to get such values, we can't an odn't use the control names. What you do get is a "object"/thing that represents the one row, and from that you can use FindConrol to pull out the value.
As noted, there would be a gallziooon instances of that control, and even different templates- so becomes impossible as to what control you trying to get.
So, for these "repeater" types of controls, you can't reference thee control directly anymore. you ALWAYS have to get the row instance, and THEN from that row instance pluck out/get/grab the control by use of FindControl.
So, the first part, the hard part and the VALUABLE part is to get that row you clicked on.
As noted, you could have/let/allow the row index changed event fire, and from that event you can then get the current row though ListView2.xxxxxx.
Howevver, from that simple button drop into the itemtemplate? (and again I STONG recommend you only have the itemtempate if you not using the lv features to edit (such as insert template).
So, in the button click event, we can get that all knowing and all valuable row. We just need that row thing, and from that we are home free.
it works like this:
protected void cmdLstSel_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
ListViewDataItem gRow = (ListViewDataItem)btn.Parent.Parent.Parent;
// ListViewDataItem gRow = (ListViewDataItem)GetParentObj(btn.Parent, typeof(ListViewDataItem));
int hID = (int)ListView1.DataKeys[gRow.DisplayIndex]["ID"];
note carful the use of Datakeys. You REALLY but REALLY want to set this if you need and want the PK row ID from the database. This also has the HUGE benefit of you not having to include the database PK id in the lv markup. (this also can be huge in terms of security issues - you don't have to include or expose the database PK id in the markup and thus it is NEVER exposed client side - do keep this in mind).
Ok, so the above gets the current row (ListViewDataItem) by using the parent property of that control clicked on. (.parent = "<td">, and we go up one more, and then one more again. So, note how I used .Parent.Parent.Parent. I even wrote a routine to do this for me, since often due to a extra div or whatever, you might have to .parent up a few more to get that row.
And of course in your case, we would do/use this:
LinkButton btn = (LinkButton)sender);
ListViewDataItem gRow = (ListViewDataItem)btn.Parent.Parent.Parent;
// ListViewDataItem gRow = (ListViewDataItem)GetParentObj(btn.Parent, typeof(ListViewDataItem));
int hID = (int)ListView1.DataKeys[gRow.DisplayIndex]["ID"];
But, ONCE we have that gRow, then you can get the value of the given text box like this:
TextBox MyTaskNameTextBox;
MyTaskNameTextBox = (TextBox)gRow.FindControl("TaskNameTextbox");
// text of box now is MyTaskNameTextBox.Text
As noted, sometimes you have to "guess" how many parents to go up to get that all valued "row". I often guess, compile and run, and the error message actually tells me you can't cast that "td" to ListViewitem, so I put in another one and compile again. But, I even became tired of that, so I wrote this routine to get the row for me:
public Control GetParentObj(Control c, Type T)
{
if (c.Parent.GetType() == T)
return c.Parent;
else
// keep looking
return GetParentObj(c.Parent, T);
}
Note above in my sample code, I commented out the use of above, but you can give this routine a try if you missing or guessing as to how many parents you have to bubble up to get the row is hit and miss for you.
As noted, you could also not use this .parent approach, and use the built in listview events, but, I often as noted, DUMP the huge mess of extra templates, and dump the HUGE complex event model of the lv, and just code my way right though this issue (to get that one all valuable row).
I dont know if the my question is good or bad or duplicate but I really want to ask you a favor.
My designer gave me grid design using html tables which looks quite handsome but when I use the same css classes in asp gridview it totally looks different.
Additional Info: In my project I have used Telerik grids. I have tried applying all the css at that too but of no use.
I cant change the designer css because its for all our company.
Now at last I want to use the same table as designer gave me and use it as grid but i really dont know how to fill it from datatable?
1. GridView generates/renders code in which it is difficult to impossible to modify.
2. I would stay away from Telerik as well.
3. Use Bootstrap CSS, you should be very happy with bootstrap css framework as you will get that look above very easily.
With Bootstrap you would use classes like class=table table-striped and you effectively have a nice grid with alternating row colors just like I see in the image that you posted.
With ASP.NET , use Nuget and install Bootstrap. Reference in Masterpage or layout.
I assume that you have styles that overwrite what your designer gave to you. Perhaps also if you are using THEIR stylesheets, make sure to overwrite the default styles. Either OMIT the style reference in master page etc... or place their stylesheet references below the default in say a asp.net web forms or mvc application. ( I assume web forms with masterpage since you are saying Gridview).
You can use ListView to include components. here is the example code for ListView.
<asp:ListView ID="ListView1" GroupPlaceholderID="group" GroupItemCount="1" ItemPlaceholderID="item" runat="server">
<LayoutTemplate>
<table>
<asp:PlaceHolder runat="server" ID="group"></asp:PlaceHolder>
</table>
</LayoutTemplate>
<GroupTemplate>
<tr>
<asp:PlaceHolder runat="server" ID="item"></asp:PlaceHolder>
</tr>
</GroupTemplate>
<ItemTemplate>
<td>
<table cellpadding="2" cellspacing="0" border="1" style="width: 200px; height: 100px">
<tr>
<td>
<span>
<%# Eval("Id") %>
</span>
</td>
</tr>
<tr>
<td>
Name: <span><%# Eval("Field_name_of_your_DB") %></span><br />
Number: <span><%# Eval("Field_name_of_your_DB") %></span><br />
Date: <span><%# Eval("Field_name_of_your_DB", "{0:MM dd, yyyy}") %></span><br />
Comment: <span><%# Eval("Field_name_of_your_DB") %></span><br />
</td>
</tr>
</table>
</td>
</ItemTemplate>
</asp:ListView>
My goal is to nest a Repeater inside an ASP.Net AJAX Accordion's AccordionPane.
So there is one Accordion which I am programmatically adding AccordionPanes to. The amount of panes I add depends on my particular dataset's count value, usually no more than 5. I've managed to do this successfully.
The thing I am having difficulty with is creating and adding a Repeater per each AccordionPane.
I've glanced over http://iridescence.no/post/Using-Templated-Controls-Programmatically.aspx but this is not exactly what I had in mind. Instead, I would rather declare a single Repeater as static HTML that I could then "clone" when I need. How can I achieve this? Obviously I would want each control's ID (within this declared Repeater) to be generated automatically each time i "clone" it.
The repeater looks like this:
<asp:Repeater ID="rptForum" runat="server">
<ItemTemplate>
<div runat="server" style="border:solid #d3d3d3 1px; border-bottom-width:0px;">
<table width="100%">
<tr><td align="left">
<asp:Label runat="server" Font-Size="12px" />
</td></tr>
<tr><td align="left">
>> <asp:Label runat="server" Font-Size="12px" Text='<%# Eval("query") %>' />
</td></tr>
</table>
</div>
</ItemTemplate>
</asp:Repeater>
I might add more fields to be databound as I progress.
Any ideas appreciated..
You should be able to do that by adding the repeater to the Content template:
<cc1:AccordionPane ID="AccordionPane1" runat="server">
<Header>
Foo
</Header>
<Content>
<asp:Repeater ID="Repeater1" runat="server" ...>
...
</asp:Repeater>
</Content>
</cc1:AccordionPane>
You can do a hierarchical data bind with the accordion, as illustrated here: http://aspalliance.com/1674_complex_data_binding_with_the_accordion_control
I have a repeater on my page which I use to display a list of search results. My issue is that the page keeps throwing me a
Parser Error Message: The server tag is not well formed.
error because the repeater has no datasource
Repeater:
<asp:Repeater runat="server" ID="rptSearchResults" >
<HeaderTemplate>
<h3>Search results</h3>
</HeaderTemplate>
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label runat="server" ID="lblTitle" Text="<%# Eval("title")%>"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lblAdress" Text="<%# Eval("adress")%>"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lblZipcode" Text="<%# Eval("zipcode")%>"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lblCity" Text="<%# Eval("city")%>"></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:Label runat="server" ID="lblType" Text="<%# Eval("type")%>"></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</asp:Repeater>
Above this repeater is a form where users can type in search words for primarily title, adress, zipcode, city and type. The repeater isn't supposed to fill out untill the user clicks the button which triggers the search and thus adds a datasource to the repeater.
Is there a way to make it work like I want it to?
I don't think the lack of a data source is the problem - it should be fine. The error says "The server tag is not well formed." - this means there's a problem with the markup. A problem with an empty data source would cause a NullReferenceException or something similar. So, maybe the problem is your Label elements - try changing the Text attributes from this:
Text="<%# Eval("type")%>"
to this:
Text='<%# Eval("type")%>'
I think all the double quotes will confuse ASP.Net. Use a combination of single and double quotes.
What happens if you disable the repeater control by default? Does it still throw the exception?
If disabling it doesn't work I'd add it dynamically as and when you need it. So that you can keep your template you can strip it out to a user control so you only have to add the user control through code and not the entire item template.
I currently have a table with a repeater that is being filled with data
<table width="100%" >
<tr>
<td class="add_border_bold" nowrap>Title</a></td>
<td class="add_border_bold" nowrap>User</td>
</tr>
<asp:Repeater id="program_list" OnItemDataBound="Repeater1_ItemDataBound" runat="server">
<ItemTemplate>
<tr>
<td class="add_border">
<%# Eval("short_title") %>
</td>
<td class="add_border">
<%# Eval("userid") %>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
I'm currently converting this from coldfusion. In the previous application, I had multiple table layouts for displaying different kinds of information, for example, phone number instead of userid. (although, working with a lot more columns, I shortened it for posting). In coldfusion I would just wrap each table in a <cfif> tag controlled by a variable that said which view to use. I am unaware how to get multiple data displays into a single page controlled by a variable, like I was before. Mostly because, it appears that most of my logic is done in the code behind files, so I don't understand how to get those changes across in the .aspx, or how to manipulate them from the code behind file.
Thanks in advance.
I would utilize a MultiView control and determine which view to show from code-behind.