I thought this would be simple, but I sure am having a lot of trouble doing this:
The title of this question may be a bit misleading. I don't have to use a gridview. In fact, I know the GridView is probably not the way to go on this. I just didn't know how else to title it. But, for now just consider:
I have a very simple class called Student. It has 4 properties:
int ID
string FirstName
string LastName
string Email
I want to keep a generic collection of these in memory (session state):
List students;
Ok, now the problem:
I want the user to create as many of these Student objects as they want. For displaying these I just want a simple table of some kind with the 3 textboxes on each row. I would like every row to have textboxes insead of labels so that any record can be edited at anytime.
When the user is finished created their student objects, then they proceed on to do other things. But, I am just having trouble finding a way to display the records this way. Do I use the ListView(3.5), html table, gridview, repeater, etc.?
How would you do it?
I would be inclined to use the ListView personally for this, since you can insert Rows with it. Your LayoutTemplate would be a table with a <tr runat="server" ID="itemPlaceHolder" /> in it. Your ItemTemplate would have your TextBox's (and optional a save button per row. Then you could have an InsertItemTemplate if you need inserts as well.
Anywhere on the page you can add a button to Save all items by looping through the ListView.Item collection and calling ListView.Update(itemIndex, validate).
<asp:ListView runat="server" ID="lv" InsertItemPosition="LastItem" DataKeyNames="id">
<LayoutTemplate>
<asp:LinkButton runat="server" OnClick="SaveAll" Text="Save All" />
<table>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
<tr runat="server" id="itemPlaceHolder" />
</table>
<asp:LinkButton runat="server" OnClick="SaveAll" Text="Save All" />
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><asp:TextBox runat="server" ID="firstName" Text='<%#Bind("firstName") %>' /></td>
<td><asp:TextBox runat="server" ID="firstName" Text='<%#Bind("lastName") %>' /></td>
<td><asp:TextBox runat="server" ID="firstName" Text='<%#Bind("email") %>' /></td>
<td><asp:LinkButton runat="server" CommandName="update" Text="Save" /></td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr>
<td><asp:TextBox runat="server" ID="firstName" Text='<%#Bind("firstName") %>' /></td>
<td><asp:TextBox runat="server" ID="firstName" Text='<%#Bind("lastName") %>' /></td>
<td><asp:TextBox runat="server" ID="firstName" Text='<%#Bind("email") %>' /></td>
<td><asp:LinkButton runat="server" CommandName="insert" Text="Save" /></td>
</tr>
</InsertItemTemplate>
</asp:ListView>
protected void SaveAll(object sender, EventArgs e)
{
lv.Items.ToList().ForEach(li => lv.UpdateItem(li.DataItemIndex, true)_;
}
Yes you could use a grid view to do this.
you could create a custom template for the columns so that they display in a textbox as opposed to a label. you can then capture the 'save' event an loop through the rows and update your data.
Gridview is going to be the easiest way to implement this. You could use an html table, but when the user wants to add more users you're going to have to do a lot more. Create a template for the gridview with your four properties (Id, FirstName, LastName, Email), and then just bind it from the session object like:
public void BindGrid()
{
// assume students is the name of your GridView control
students.DataSource = (List<Student>)Session["StudentList"];
students.DataBind();
}
Yes, this is possible. This is just a code sample for what Victor had posted.
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="False" DataKeyNames="PartNumber" GridLines="Horizontal"
ForeColor="#333333" CellPadding="4" Width="800" PageSize="20" OnDataBound="gvPartDetails_DataBound" OnSelectedIndexChanged="gvPartDetails_SelectedIndexChanged" OnSorting="gvPartDetails_Sorting">
<Columns>
<asp:TemplateField HeaderText="#">
<ItemTemplate>
<asp:Label ID="lblNumber" runat="server" Text='<%# Bind("Number") %>' ToolTip='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
<HeaderStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="LOC 1">
<ItemTemplate>
<asp:TextBox ID="txt_Qty1" AutoPostBack="false" runat="server" MaxLength="5" Width="50" Text='<%# Bind("Qty1") %>'></asp:TextBox>
</ItemTemplate>
<HeaderStyle Wrap="False" />
</asp:TemplateField>
<asp:TemplateField HeaderText="LOC 2">
<ItemTemplate>
<asp:TextBox ID="txt_Qty2" AutoPostBack="false" runat="server" MaxLength="5" Width="50" Text='<%# Bind("Qty2") %>'></asp:TextBox>
</ItemTemplate>
<HeaderStyle Wrap="False" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<span id="Empty">No Details For This Such and Such.</span>
<img src="Images/icons/table_add.png" style="border:0px" alt="Add New Part" />
</EmptyDataTemplate>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#284775" Font-Bold="True" ForeColor="White" HorizontalAlign="Left" />
<AlternatingRowStyle BackColor="Gainsboro" ForeColor="#284775" />
</asp:GridView>
And then the code behind... this is very rough...
private void ApplyChanges()
{
foreach (GridViewRow row in this.gvDetails.Rows)
{
//do something with cells and data objects
//and then save add to list, save, etc.
}
}
EDIT
Above is where you will get your editing of the data. You can work with DataTables, DataViews, DataSets, and as the other solution shows, you can bind your grid to a list of your object. This should be determined based upon the data object model in your system and how the rows would be generated.
Related
In a page I have a grid and 2 drop down lists inside a update panel. From drop down list user will select and selected item will saved and shown in update panel. I am also planning to use updateprogress with it.
On doing this I am getting this design time error " 'System.Web.UI.WebControls.ImageButton' does not have a public property named 'UpdateProgress'"
I have spent a lot of time on it but couldn't solve it. Please help me. Here is my code for this.
<tr>
<td colspan="3">
<div>
<asp:Label runat="server" ID="Label1" Text="Update your professional experience below"></asp:Label>
<asp:UpdatePanel runat="server" ID="upExperience">
<ContentTemplate>
<asp:DropDownList runat="server" AutoPostBack="true" ID="ddlCategory">
</asp:DropDownList>
<asp:DropDownList runat="server" ID="ddlValues">
</asp:DropDownList>
<asp:Button runat="server" ID="btnAddNew" Text="Add New" />
<asp:GridView runat="server" ID="gvExperience" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Category" HeaderText="Category" />
<asp:BoundField DataField="Experience" HeaderText="Experience" />
<asp:BoundField DataField="Status" HeaderText="Status" />
<asp:TemplateField HeaderText="Remove">
<ItemTemplate>
<asp:ImageButton runat="server" ID="btnRemove" CommandArgument="<%#Eval("ID")%>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdateProgress runat="server" ID="upp">
<ProgressTemplate>
<img src="../Images/loading.gif" />
</ProgressTemplate>
</asp:UpdateProgress>
</div>
</td>
</tr>
Your ImageButton server control is not well formed because of the double qoutes surroundings the Eval, so the asp:UpdateProgress is being seen as the continuation of the tag. Replace with this:
<asp:ImageButton runat="server" ID="btnRemove" CommandArgument='<%#Eval("ID")%>' />
In Asp.Net, how to apply text align [left,center,right] in a column, when autogenerate column is true. In every row of gridview the text is displayed in center of the column, i want to display in left side of the column. Thank you.
Try using below syntax if all the columns needs to left aligned
<RowStyle HorizontalAlign="Left"></RowStyle>
Also check these MSDN resource which has various examples for GridView formatting and Code Project article which shows examples for AutoGenerated Columns scenerio.
Sorry, this case will work only when autogeneratedcolumns = "false". See the edit for the autogeneratecolumns="true" If you want to align the header of the column add this to the BoundFiled:
HeaderStyle-HorizontalAlign="Right"
And if you want to align the item of the column add this to the BoundField:
ItemStyle-HorizontalAlign="Right"
EDIT:
Also try this too
Click on the gridView->properties->RowStyle:horizontal-align
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
allowpaging="true"
ondatabound="CustomersGridView_DataBound"
runat="server">
<pagerstyle forecolor="Blue"
backcolor="LightBlue"/>
<pagertemplate>
<table width="100%">
<tr>
<td style="width:70%">
<asp:label id="MessageLabel"
forecolor="Blue"
text="Select a page:"
runat="server"/>
<asp:dropdownlist id="PageDropDownList"
autopostback="true"
onselectedindexchanged="PageDropDownList_SelectedIndexChanged"
runat="server"/>
</td>
<td style="width:70%; text-align:right">
<asp:label id="CurrentPageLabel"
forecolor="Blue"
runat="server"/>
</td>
</tr>
</table>
</pagertemplate>
</asp:gridview>
more info on this link
You can define <RowStyle> and <HeaderStyle> elements in your markup.
Example:
<asp:GridView ID="productGridView" Runat="server"
DataSourceID="productsDataSource"
DataKeyNames="ProductID" AutoGenerateColumns="False"
BorderWidth="1px" BackColor="#DEBA84"
CellPadding="3" CellSpacing="2" BorderStyle="None"
BorderColor="#DEBA84">
<FooterStyle ForeColor="#8C4510"
BackColor="#F7DFB5"></FooterStyle>
<PagerStyle ForeColor="#8C4510"
HorizontalAlign="Center"></PagerStyle>
<HeaderStyle ForeColor="White" Font-Bold="True"
BackColor="#A55129"></HeaderStyle>
</asp:GridView>
Put your columns as template fields as per below and give ItemStyle-HorizontalAlign="Left"
<Columns>
<asp:TemplateField ItemStyle-Width="5%" HeaderText="No." ItemStyle-
HorizontalAlign="Left">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
So it will work in all browser.
I am facing little issue in my web application in asp.net.
i am receiving the below error :
Error: Sys.InvalidOperationException: Handler was not added through the Sys.UI.DomEvent.addHandler method.
I have used Updatepanel and this error occurs when i try to do some 2-3 actions very quickly.
and when next time i try to do take some action my web application just hungs up.
Please suggest.
Thanks
In my case this was caused by having the 'Close' control within an Update Panel in the Modal Popup. I fixed it by creating a 'dummy' button outside of the Update Panel, and setting it as the 'CancelControlID' in the MPE attributes:
<asp:Button ID="btnDummyCloseWindow" runat="server" Style="visibility: hidden"/>
<ajaxToolkit:ModalPopupExtender ID="mpeWindow" runat="server" PopupControlID="pnlWindow"
TargetControlID="btnDummyOtherButton" BackgroundCssClass="modalBackground"
DropShadow="false" CancelControlID="btnDummyCloseWindow" />
You'll need to make sure the close button that is present within your Update Panel has actions assigned to it to close the window (e.g. mpeWindow.hide()).
It's also worth noting that I was also making use of the TargetControlID 'fix' too, where a dummy button is referenced, so ignore the TargetControlID attribute there.
I've solved the problem setting ScriptMode property of ScriptManager to Release instead of Debug
By default ScriptManager is set to Debug mode.
I had the same problem and solved by placing the ModalPopupExtender or the user control that uses ModalPopupExtender inside an update panel.
Which ever way you want to look at it, the problem is inherit in what I believe is a bug in AJAX.
The only way I was able to resolve this was to control your Sorting or Paging on the server side where you control or more specifically refresh the UpdatePanel along with making sure the ModalPopup has been kept visible!
The reason for the error is really because once you do a sort of page change on a GridView that's inside an UpdatePanel, the controls have been "lost" to the UpdatePanel.
A better explanation is here.
Here is a column from my GridView...
<asp:GridView ID="gvTETstudents" runat="server" AutoGenerateColumns="False" AllowSorting="True" CellPadding="4" ForeColor="#333333" Font-Size="Small" Width="100%"
DataSourceID="sdsTETstudents"
OnRowCreated="gvTETstudents_RowCreated"
OnRowDataBound="gvTETstudents_RowDataBound"
OnDataBound="gvTETstudents_DataBound">
<Columns>
..
..
<ItemTemplate>
<asp:UpdatePanel ID="upWEF1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnWEFCLOSE" />
</Triggers>
<ContentTemplate>
...
...
<asp:Panel ID="pnlWEF2" runat="server" style="display:none;">
<table><tr><td>
<asp:Button ID="btnWEFshow" runat="server"
Text="ALL"
Font-Size="Small" Font-Names="Arial"
ToolTip="Click here to see all of this student's work experience feedback on file" />
<ajaxToolkit:ModalPopupExtender ID="mpeWEF" runat="server"
BackgroundCssClass="modalBackground"
OkControlID="btnWEFCLOSE"
PopupControlID="upWEF2"
TargetControlID="btnWEFshow">
</ajaxToolkit:ModalPopupExtender>
<asp:UpdatePanel ID="upWEF2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel ID="pnlWEF3" runat="server" CssClass="pnlEndorsed">
<div id="Hdr" style="text-align: center">
<asp:Label ID="lblWEFHdr" runat="server">** CONTACT LOG **</asp:Label>
</div>
<div id="Bdy">
<table style="width:100%"><tr><td>
<asp:GridView ID="gvWEFContactLog" runat="server"
Font-Size="X-Small" CellPadding="4" ForeColor="#333333" GridLines="None" AllowPaging="true" PageSize="8" AllowSorting="True" AutoGenerateColumns="False" Width="100%"
DataKeyNames="StudentContactLogID,Private,ApprenticeContactLogID"
DataSourceID="sdsWEF"
OnRowDataBound="gvWEFContactLog_RowDataBound"
OnPageIndexChanging="gvWEFContactLog_PageIndexChanging"
OnSorted="gvWEFContactLog_Sorted">
<Columns>
<asp:TemplateField HeaderText="First Entered" SortExpression="FirstEntered">
<ItemTemplate>
<asp:HiddenField ID="hfWEFStudCLid" runat="server" Value='<%# Eval("StudentContactLogID") %>' />
<asp:HiddenField ID="hfWEFAppCLid" runat="server" Value='<%# Eval("ApprenticeContactLogID") %>' />
<asp:HiddenField ID="hfPrivate" runat="server" Value='<%# Eval("Private") %>' />
<asp:HiddenField ID="hfNotes" runat="server" Value='<%# Eval("ContactNotes") %>' />
<asp:LinkButton ID="lnkWEFCLOG" runat="server"
Text='<%# Eval("FirstEntered","{0:d MMM yyyy HH:mm}") %>'></asp:LinkButton>
<a id="lnkDummy" runat="server" visible=false></a>
<ajaxToolkit:ModalPopupExtender ID="mpeWEFCLOG" runat="server"
OkControlID="btnWEFCLOSEview"
PopupControlID="upWEFCLOG"
TargetControlID="lnkWEFCLOG">
</ajaxToolkit:ModalPopupExtender>
<asp:UpdatePanel ID="upWEFCLOG" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div id="pnlWEFCLOG" runat="server" class="pnlCLOG">
<asp:TextBox ID="txtWEFCLOG" runat="server"
Wrap="true"
TextMode="MultiLine"
Rows="10"
ReadOnly="true"
Width="98%">
</asp:TextBox>
<br />
<asp:Button ID="btnWEFCLOSEview" runat="server" Text="OK" Width="100%" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Subject" HeaderText="Subject" SortExpression="Subject" />
<asp:BoundField Visible="False" DataField="StudentContactLogID" HeaderText="LogID"
InsertVisible="False" ReadOnly="True" SortExpression="StudentContactLogID">
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField DataField="StaffName" HeaderText="Staff" ReadOnly="True" SortExpression="StaffName" />
<asp:TemplateField HeaderText="Contact Date Time" SortExpression="ContactDateTime">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("ContactDateTime","{0:d MMM yyyy HH:mm}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Follow-Up Date" SortExpression="FollowUpDate">
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("FollowUpDate","{0:d MMM yyyy}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Private" HeaderText="Private" SortExpression="Private" />
</Columns>
<EmptyDataTemplate>
No Current Entries
</EmptyDataTemplate>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<asp:SqlDataSource ID="sdsWEF" runat="server" ConnectionString="<%$ ConnectionStrings:ATCNTV1ConnectionString %>"
SelectCommand="spTETStudentContactLogView" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:Parameter Name="StudentID" Type="string" />
<asp:Parameter Name="WEF" Type="string" DefaultValue="%" />
</SelectParameters>
</asp:SqlDataSource>
</td></tr>
<tr style="text-align: center">
<td style="text-align: left">
<asp:Button ID="btnWEFCLOSE" runat="server"
Text="CLOSE"
CausesValidation="false" Font-Bold="True" Width="61px" />
</td>
</tr>
</table>
</div>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</td></tr></table>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
The major point of the code above is that I have a very deep GridView inside an UpdatePanel that's inside a ModalPopUp.
Now look at what I have inside that GridView:
OnPageIndexChanging
and
OnSorted
Inside the GridView, there is another ModalPopup and TextBox. Ignore that. That's only so the user can see the comments from the student's contact log as another popup window.
So if we now go to the code behind for the above two events:
protected void gvWEFContactLog_Sorted(object sender, EventArgs e)
{
GridView gvWEFCL = (GridView)sender;
GridViewRow gvRow = (GridViewRow)gvWEFCL.NamingContainer;
UpdatePanel upWEF1 = (UpdatePanel)gvRow.FindControl("upWEF1");
if (upWEF1 != null) upWEF1.Update();
AjaxControlToolkit.ModalPopupExtender mpeWEF = (AjaxControlToolkit.ModalPopupExtender)gvRow.FindControl("mpeWEF");
if (mpeWEF != null) mpeWEF.Show();
}
protected void gvWEFContactLog_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView gvWEFCL = (GridView)sender;
GridViewRow gvRow = (GridViewRow)gvWEFCL.NamingContainer;
UpdatePanel upWEF1 = (UpdatePanel)gvRow.FindControl("upWEF1");
if (upWEF1 != null) upWEF1.Update();
AjaxControlToolkit.ModalPopupExtender mpeWEF = (AjaxControlToolkit.ModalPopupExtender)gvRow.FindControl("mpeWEF");
if (mpeWEF != null) mpeWEF.Show();
}
Notice that I am not actually controlling the sorting or the paging itself. I am only forcing the GridView to call upon the main UpdatePanel (upWEF1) to refresh itself via an Update() call. The next step is to grab the ModalPopup I want to keep visible and re-Show() it!
And that's all there is to it!
I am sure there is a cleaner solution using JavaScript itself, but for me this avoids that dread meaningless error and I have a clean set of popups and update panels that can handle both hotlinks, sorting and paging as I want the GridView to perform!
I have a repeater which contains a nested gridview. Now I need to be able to retrieve a value databound in a label inside of a repeater and use it as an input parameter for the gridview.
Here is some code which will hopefully give you a better idea of what I'm trying to do.
enter code here
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="RepeaterDS" OnItemDataBound="Repeater1_SendRollNumber">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<table style="width: 615px;" id="Table1">
<tr>
<td>
<b>Roll #:</b>
<asp:Label runat="server" ID="RollIDLabel" Text='<%# Eval("RollID") %>' />
<asp:Label runat="server" ID="RollIDLabelCode" Text='<%# Eval("RollID") %>' Visible="false" />
</td>
</tr>
<tr>
<td>
<b>Address:</b>
<asp:Label runat="server" ID="AddressLabel" Text='<%# Eval("Address") %>' />
</table>
<asp:Label ID="Label1" runat="server"><b>Parties:</b></asp:Label>
<asp:GridView ID="GridView3" runat="server" BackColor="White" BorderColor="#DEDFDE"
BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical"
AllowPaging="True" Width="620px" DataSourceID="AssessmentDetailsFromRollIDDS"
EmptyDataText="None Associated" AutoGenerateColumns="False" ShowFooter="true">
<asp:TemplateField HeaderText="Property Assessment" HeaderStyle-Font-Bold="true"
FooterStyle-BackColor="White">
<ItemTemplate>
<asp:Label ID="PropAssessType" runat="server" Width="90%" Text='<%# Eval("PropertyAssessmentType") %>'></asp:Label>
<asp:Label ID="PropAssessDsc" runat="server" Text='<%# Eval("PropertyAssessmentDesc") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:TemplateField>
<RowStyle BackColor="#F7F7DE" />
<FooterStyle BackColor="#CCCC99" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#3E4E4E" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
</ItemTemplate>
</asp:Repeater>
<asp:ObjectDataSource ID="RepeaterDS" runat="server" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetRollNumberbyAppealID" TypeName="BusinessLayer.BSO.Roll_AssessmentDetailsBSO">
<SelectParameters>
<asp:QueryStringParameter Name="AppealID" QueryStringField="appealID" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ObjectDataSource ID="AssessmentDetailsFromRollIDDS" runat="server" OldValuesParameterFormatString="original_{0}"
SelectMethod="GetAssessDetailsFromRollID" OnSelecting="AssessmentDetailsFromRollIDDS_Selecting" TypeName="BusinessLayer.BSO.Assessment_DetailsBSO">
<SelectParameters>
<asp:ControlParameter Name="RollID" ControlID="RollIDLabelCode" Type="String" />
</SelectParameters>
</asp:ObjectDataSource>
I've tried to cut out as much code as possible while leaving the guts of the problem in. So I basically want the label control RollIDLabelCode which is located in a repeater to also be an input into my gridview. The problem is that I keep getting errors such as can't find the control RollIDLabelCode. I've heard there is a bug in which you need to specify all naming containers. which I've tried with no luck.
Another route I've tried is doing this in the code behind.
enter code here
public void Repeater1_OnItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
// Execute the following logic for Items and Alternating Items.
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string test = ((Label)e.Item.FindControl("RollIDLabel")).Text;
}
}
public void AssessmentDetailsFromRollIDDS_OnSelecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters["RollID"] = "Need the Info here";
}
These functions are called on the repeater being databound and the objectdatasource onselecting function. And these work separately very well. I just need to know how to get the information from the string test in the Repeater1_OnItemDataBound function to the e.InputParameters["RollID"] in the OnSelecting function.
Hopefully someone knows how to do this. I am new to .net programming. I appreciate any help.
Thanks!
The RollIDLabelCode label is set visible=false which means it will not exist when rendered so trying to access the value won't be possible. You'll need to use a different control that is hidden but exists when rendered. For instance a GridView has a datakeys that exist within the control but are not rendered. It is used to retrieve a key value for the row to retreive more data, for example.
I have an ASP.NET GridView that uses an EmptyDataTemplate. This template is used to collect data in the event that no records exist in my data source. My GridView source looks like this:
<asp:GridView ID="myGridView" runat="server"
DataKeyNames="ID" OnRowEditing="myGridView_RowEditing"
OnRowCancelingEdit="myGridView_RowCancelingEdit"
OnRowUpdating="myGridView_RowUpdating"
ShowFooter="True" EnableModelValidation="True">
<Columns>
<asp:BoundField DataField="ID" Visible="false" />
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="nameFooterTextBox" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Age">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Age") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Age") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="ageTextBox" runat="server" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Options" HeaderStyle-HorizontalAlign="Left"
ButtonType="Link" ShowEditButton="true" ShowDeleteButton="true" >
</asp:CommandField>
</Columns>
<EmptyDataTemplate>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Name</td>
<td>Age</td>
<td>Options</td>
</tr>
<tr>
<td><asp:TextBox ID="nameTextBox" runat="server" /></td>
<td><asp:TextBox ID="ageTextBox" runat="server" /></td>
<td><asp:LinkButton ID="saveLinkButton" runat="server" Text="save" OnClick="saveLinkButton_Click" /></td>
</tr>
</table>
</EmptyDataTemplate>
When a user clicks the "saveLinkButton" in the EmptyDataTemplate, I want to get the values from the TextBoxes and insert a new record into my data source. My question is, how do I get the values of those text fields when somebody clicks the "saveLinkButton"?
Thank you!
This thread over at asp.net proposes a solution to this problem (Note: I haven't tried it out)
http://forums.asp.net/p/1436652/3240106.aspx
You need to handle the RowCommand event, get the parent naming container of the control that raise the event (your linkbutton) and then search for the textboxes using FindControl within that.
Try this to get the values of the name and age in row command of the grid..
Before doing this set the command name of the saveLinkbutton to MyInsert and remove the onlcik event as u may not need it.
protected void yourGridview_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("MyInsert"))
{
TextBox nameTextBox= gvEligibility.Controls[0].Controls[0].FindControl("nameTextBox") as TextBox;
TextBox ageTextBox= gvEligibility.Controls[0].Controls[0].FindControl("ageTextBox") as TextBox;
string name=nameTextBox.Text();
string age=ageTextBox.Text();
//save code here
}
}