ListView Validation not happenning on ItemInserting - c#

I have a Listview with a small validation on ItemInserting.
The problem is that it isn't working.
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
</td>
<td> </td>
<td>
<asp:TextBox ID="ProgrammeTextBox" runat="server" Text='<%# Bind("Programme") %>' />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Required" ControlToValidate="ProgrammeTextBox" Enabled="false"></asp:RequiredFieldValidator><br />
</td>
<td>
<asp:CheckBox ID="OutcomeCheckBox" runat="server" Checked='<%# Bind("Outcome") %>' />
</td>
</tr>
</InsertItemTemplate>
protected void ListView1_ItemInserting(object sender, ListViewInsertEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
CheckBox chk = e.Item.FindControl("OutcomeCheckBox") as CheckBox;
RequiredFieldValidator RFV1 = e.Item.FindControl("RequiredFieldValidator1") as RequiredFieldValidator;
if (chk.Checked)
{
RFV1.Enabled = true;
}
else
{
RFV1.Enabled = false;
}
}
}
The ListView1_ItemInserting isn't applying the Required Field Validator when the checkbox is checked.
Any ideas???
I cant work out why this is not firing.

Related

ASP.net listview dropdown item getting the value from another table

I am trying to build an admin page in order to let the user create edit update the records of the specific table. "District" table has the following properties: DistrictID, DistrictName, Description, DistrictImage, CityFK. Since CityFK is just numbers which represents CityID of the table "City" and does not mean anything to the user I have used a dropdown menu instead and it displays all city names as "DataTextField" and CityIDs as "DataValueField" from the table "City". But my aim is to insert that "DataValueField" into the District table as CityFK. But whenever I enter a new District record and choose a City from the dropdown and press "Insert" button of the listview, CityFK of that regarding record is "0".
I paste the whole listview but if you look at the dropdownlist at the InsertItemTemplate or EditItemTemplate you will what I mean;
<asp:ListView ID="DistrictList" runat="server" DataKeyNames="DistrictID" InsertItemPosition="LastItem" DataSourceID="DistrictEntityDataSource" >
<AlternatingItemTemplate>
<tr style="background-color:#FFF8DC;">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="DistrictIDLabel" runat="server" Text='<%# Eval("DistrictID") %>' />
</td>
<td>
<asp:Label ID="DistrictNameLabel" runat="server" Text='<%# Eval("DistrictName") %>' />
</td>
<td>
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
</td>
<td>
<asp:Image ID="DistrictImageLabel" runat="server" Height="100px" ImageUrl='<%# "~/Handlers/ImageHandler.ashx?ID="+Eval("DistrictID")+"&Entity=District"%>'/>
</td>
<td>
<asp:Label ID="CityFKLabel" runat="server" Text='<%# Eval("CityFK") %>' />
</td>
</tr>
</AlternatingItemTemplate>
<EditItemTemplate>
<tr style="background-color:#008A8C;color: #FFFFFF;">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label ID="DistrictIDLabel1" runat="server" Text='<%# Eval("DistrictID") %>' />
</td>
<td>
<asp:TextBox ID="DistrictNameTextBox" runat="server" Text='<%# Bind("DistrictName") %>' />
</td>
<td>
<asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>' />
</td>
<td>
<asp:FileUpload ID="FileUploadDistrict2" runat="server" />
</td>
<td>
<asp:DropDownList ID="CityFKDropDownList" runat="server" AutoPostBack="true" DataSourceID="ddlCityFK_DataSource" ViewStateMode="Enabled"
DataTextField="CityName" DataValueField="CityID"
AppendDataBoundItems="true">
<asp:ListItem Text="-Stadt Wählen-" Value="0" ></asp:ListItem>
</asp:DropDownList>
<asp:EntityDataSource ID="ddlCityFK_DataSource" runat="server"
ConnectionString="name=MedicalEntities" DefaultContainerName="MedicalEntities"
EntitySetName="Cities" >
</asp:EntityDataSource>
</td>
</tr>
</EditItemTemplate>
<EmptyDataTemplate>
<table runat="server" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
<tr>
<td>No data was returned.</td>
</tr>
</table>
</EmptyDataTemplate>
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
</td>
<td>
<asp:TextBox ID="DistrictIDTextBox" runat="server" Text='<%# Bind("DistrictID") %>' />
</td>
<td>
<asp:TextBox ID="DistrictNameTextBox" runat="server" Text='<%# Bind("DistrictName") %>' />
</td>
<td>
<asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>' />
</td>
<td>
<asp:FileUpload ID="FileUploadDistrict" runat="server"/>
</td>
<td>
<asp:DropDownList ID="CityFKDropDownList" runat="server" AutoPostBack="true" DataSourceID="ddlCityFK_DataSource" ViewStateMode="Enabled"
DataTextField="CityName" DataValueField="CityID"
AppendDataBoundItems="true">
<asp:ListItem Text="-Stadt Wählen-" Value="0" ></asp:ListItem>
</asp:DropDownList>
<asp:EntityDataSource ID="ddlCityFK_DataSource" runat="server"
ConnectionString="name=MedicalEntities" DefaultContainerName="MedicalEntities"
EntitySetName="Cities">
</asp:EntityDataSource>
</td>
</tr>
</InsertItemTemplate>
<ItemTemplate>
<tr style="background-color:#DCDCDC;color: #000000;">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="DistrictIDLabel" runat="server" Text='<%# Eval("DistrictID") %>' />
</td>
<td>
<asp:Label ID="DistrictNameLabel" runat="server" Text='<%# Eval("DistrictName") %>' />
</td>
<td>
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
</td>
<td>
<asp:Image ID="DistrictImageLabel" runat="server" Height="100px" ImageUrl='<%# "~/Handlers/ImageHandler.ashx?ID="+Eval("DistrictID")+"&Entity=District"%>'/>
</td>
<td>
<asp:Label ID="CityFKLabel" runat="server" Text='<%# Eval("CityFK") %>' />
</td>
</tr>
</ItemTemplate>
<LayoutTemplate>
<table runat="server">
<tr runat="server">
<td runat="server">
<table id="itemPlaceholderContainer" runat="server" border="1" style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
<tr runat="server" style="background-color:#DCDCDC;color: #000000;">
<th runat="server"></th>
<th runat="server">DistrictID</th>
<th runat="server">DistrictName</th>
<th runat="server">Description</th>
<th runat="server">DistrictImage</th>
<th runat="server">CityFK</th>
</tr>
<tr id="itemPlaceholder" runat="server">
</tr>
</table>
</td>
</tr>
<tr runat="server">
<td runat="server" style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Arial, Helvetica, sans-serif;color: #000000;">
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</td>
</tr>
</table>
</LayoutTemplate>
<SelectedItemTemplate>
<tr style="background-color:#008A8C;font-weight: bold;color: #FFFFFF;">
<td>
<asp:Button ID="DeleteButton" runat="server" CommandName="Delete" Text="Delete" />
<asp:Button ID="EditButton" runat="server" CommandName="Edit" Text="Edit" />
</td>
<td>
<asp:Label ID="DistrictIDLabel" runat="server" Text='<%# Eval("DistrictID") %>' />
</td>
<td>
<asp:Label ID="DistrictNameLabel" runat="server" Text='<%# Eval("DistrictName") %>' />
</td>
<td>
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
</td>
<td>
<asp:Image ID="DistrictImageLabel" runat="server" Height="100px" ImageUrl='<%# "~/Handlers/ImageHandler.ashx?ID="+Eval("DistrictID")+"&Entity=District"%>'/>
</td>
<td>
<asp:Label ID="CityFKLabel" runat="server" Text='<%# Eval("CityFK") %>' />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<asp:EntityDataSource ID="DistrictEntityDataSource" runat="server" ConnectionString="name=MedicalEntities"
DefaultContainerName="MedicalEntities" EnableDelete="True" EnableFlattening="False" EnableInsert="True"
EnableUpdate="True" EntitySetName="Districts" Include="City" EntityTypeFilter="District">
</asp:EntityDataSource>
So I am trying to bind that dropdownlist values to the CityFK during Insert and Edit operations.
I figured it out. As workaround I added one textbox at the same column with the dropdownlist and set its visible attribute to false;
<asp:TextBox ID="CityFKTextBoxInsert" runat="server" Visible="false" Text='<%# Bind("CityFK") %>' />
In case you also encounter such a problem I add the code-behind and asp side here;
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
</td>
<td>
<asp:TextBox ID="DistrictIDTextBox" runat="server" Text='<%# Bind("DistrictID") %>' />
</td>
<td>
<asp:TextBox ID="DistrictNameTextBox" runat="server" Text='<%# Bind("DistrictName") %>' />
</td>
<td>
<asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>' />
</td>
<td>
<asp:FileUpload ID="FileUploadDistrictInsert" runat="server" />
</td>
<td>
<asp:TextBox ID="CityFKTextBoxInsert" runat="server" Visible="false" Text='<%# Bind("CityFK") %>' />
<asp:DropDownList ID="CityFKDropDownListInsert" runat="server" AutoPostBack="true" DataSourceID="ddlCityFK_DataSource2" ViewStateMode="Enabled"
DataTextField="CityName" DataValueField="CityID" OnSelectedIndexChanged="CityFKDropDownListInsert_SelectedIndexChanged"
AppendDataBoundItems="true">
<asp:ListItem Text="-Stadt Wählen-" Value="0" ></asp:ListItem>
</asp:DropDownList>
<asp:EntityDataSource ID="ddlCityFK_DataSource2" runat="server"
ConnectionString="name=MedicalEntities" DefaultContainerName="MedicalEntities"
EntitySetName="Cities" >
</asp:EntityDataSource>
</td>
</tr>
</InsertItemTemplate>
Then I have added a "OnSelectedIndexChanged" event to the dropdown and assign the selected value to that textbox;
protected void CityFKDropDownListInsert_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList DropdownDistrict = (DropDownList)DistrictList.InsertItem.FindControl("CityFKDropDownListInsert");
TextBox DistrictTextBox = (TextBox)DistrictList.InsertItem.FindControl("CityFKTextBoxInsert");
DistrictTextBox.Text = DropdownDistrict.SelectedValue;
}
If you want to apply that solution also to EditItemTemplate only thing you have to do changing that line;
DropDownList DropdownDistrict = (DropDownList)DistrictList.InsertItem.FindControl("CityFKDropDownListInsert");
to this;
DropDownList DropdownDistrict = (DropDownList)DistrictList.EditItem.FindControl("CityFKDropDownListInsert");

How to set a DataList.SelectedItem equal to a uniqeItemId

I have a list of objects called dashboard which holds a list of Announcements with their AnnouncementId. My DataListAnnouncements.DataSource is equal to the dashboard but I can't figure out how to get the selectedItem equal to the item that I am selecting in the client side.
What Currently is happening is when I select an unread announcement, it displays the message with some css, but it does that to the wrong announcement at the moment.
protected void DataListAnnouncements_ItemCommand(object source, DataListCommandEventArgs e)
{
if (e.CommandName == "Select")
{
IList<DashBoardView> dashboard = new List<DashBoardView>();
dashboard = (IList<DataObjects.DashBoardView>)ListOfObjects;
UnreadAnnouncement unread = UnreadAnnouncements;
if (e.CommandArgument != null)
{
if (unread != null)
{
unread.WasRead = true;
UpdateUnreadAnnouncement(unread);
att = unread.AnnouncementId;
}
}
dashboard[e.Item.ItemIndex].WasRead = unread.WasRead;
DataListAnnouncements.DataSource = Session["dashboard"];
DataListAnnouncements.SelectedItem = dashboard[e.Item.ItemIndex]
(DataListAnnouncements.SelectedItem = att <---- Something Like this is what I would like to implement?)
Session["dashboard"] = dashboard;
bindDataList();
}
}
as you see now, I have it using the dashboard[e.Item.ItemIndex].WasRead = unread.WasRead line, but this proves to not work since the index is not always what I want. So I want to use an attribute. I looked around a bit but I am still fuzzy on how to implement it on the C# and asp side.
and this is my ASP.
<ItemTemplate>
<table width="880px">
<tr>
<td class="leftCol">
<asp:LinkButton Text='<%# Eval("title") %>' CssClass="bold" runat="server" CommandName="Select" CommandArgument='<%# Eval("UnreadAnnouncementId") %>' ID="titleLabel" /></span>
</td>
<td class="created">
<asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "Effective", "{0:MM/dd/yyyy}") %>' CssClass="created" runat="server" ID="Label4" />
<br />
<asp:Label Text='<%# Eval("FirstName") %>' runat="server" ID="Label2" />
<asp:Label Text='<%# Eval("LastName") %>' runat="server" ID="Label3" />
</td>
</tr>
<tr>
<td class="one-long-line">
<asp:Label Text='<%# Eval("details") %>' CssClass="details" runat="server" ID="detailsLabel" />
</td>
</tr>
</table>
</ItemTemplate>
<SelectedItemTemplate>
<table width="880px" cellpadding="10px">
<tr>
<td class="leftCol">
<asp:Label Text='<%# Eval("title") %>' CssClass="bold" runat="server" ID="titleLabel" />
<asp:Label runat="server" ID="wasRead" Text='<%# Eval("wasRead") %>' Visible="false" Enabled="false" />
<td class="created">
<asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "Effective", "{0:MM/dd/yyyy}") %>' CssClass="created" runat="server" ID="Label4" />
<br />
<asp:Label Text='<%# Eval("FirstName") %>' runat="server" ID="Label2" />
<asp:Label Text='<%# Eval("LastName") %>' runat="server" ID="Label3" />
</td>
</tr>
<tr>
<td class="deailsTD" colspan="2">
<asp:Label Text='<%# Eval("details") %>' CssClass="details" runat="server" ID="detailsLabel" />
</td>
</tr>
<tr>
</tr>
</table>
</SelectedItemTemplate>
I want to be able to click on a item in the datagrid, and it will expand that item and show me the details.
You have to use `SelectedIndex', not SelectedItem'. Source: How to: Allow Users to Select Items in DataList Web Server Controls.
It could be like this:
DataListAnnouncements.SelectedIndex = e.Item.ItemIndex;
From your code it is not clear what is the relation between dashboard and att. If there's any way to find the att in dashboard and find it's position, you can do like this:
DataListAnnouncements.SelectedIndex = attposition;
EDIT : There's an easy way to control background color of DataLists Items. We can parse each item in DataList's ItemBound and set BackColor like below:
protected void DataListAnnouncements_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem||
e.Item.ItemType == ListItemType.SelectedItem
)
{
// Here BackColor - Grey: WasRead; Yellow: Unread
var wasRead = ((DashBoardView)e.Item.DataItem).WasRead;
e.Item.BackColor = wasRead? System.Drawing.Color.Gray: System.Drawing.Color.Yellow;
}
}
You can even expand it more by splitting the if condition into two:
if (e.Item.ItemType == ListItemType.Item ||e.Item.ItemType == ListItemType.AlternatingItem){}
and
if ( e.Item.ItemType == ListItemType.SelectedItem){}

Dropdown has a selected value which is invalid

I have been working on a project and facing an error as i have two textbox and a dropdown when i fill one of the textbox on its selecting_indexchanged the dropdown get filled,what is happening is when i click on reset button to reset the dropdown a textboxes to be blank and dropowns index value to 0,its shows as error as
"dropdown has a SelectedIndex which is invalid because it does not exist in the list of items.
Parameter name: value "
<td class="style4">
<asp:Label ID="lbl_bookname" runat="server" Text="Book Name"
ForeColor="Black"></asp:Label>
</td>
<asp:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txt_bookname" MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1" CompletionInterval="1000" ServiceMethod="getbookname">
</asp:AutoCompleteExtender>
<td class="style4">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:TextBox ID="txt_bookname" runat="server" ValidationGroup="a" OnTextChanged="editiondrpfill_TextChanged" AutoPostBack="true"></asp:TextBox>
<asp:AutoCompleteExtender ID="AutoCompleteExtender2" runat="server" TargetControlID="txt_bookname" MinimumPrefixLength="1" EnableCaching="true" CompletionSetCount="1" CompletionInterval="1000" ServiceMethod="getbookname">
</asp:AutoCompleteExtender>
</td>
<td class="style4">
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ErrorMessage="*" ForeColor="Red" ControlToValidate="txt_bookname"
ValidationGroup="a"></asp:RequiredFieldValidator>
</td>
<td class="style4">
<asp:Label ID="lbl_condition" runat="server" ForeColor="Black" Text="Condition"></asp:Label>
</td>
<td class="style4">
<asp:TextBox ID="txt_condition" runat="server" BackColor="White"
ReadOnly="True" Width="120px"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server"
ControlToValidate="txt_condition" ErrorMessage="*" ForeColor="Red"
ValidationGroup="a"></asp:RequiredFieldValidator>
</td>
<td>
</td>
</tr>
<tr>
<td class="style4">
<asp:Label ID="lbl_edition" runat="server" ForeColor="Black" Text="Edition"></asp:Label>
</td>
<td class="style4">
<asp:DropDownList ID="drp_edition" runat="server" ValidationGroup="a"
Width="120px" onselectedindexchanged="drp_edition_SelectedIndexChanged"
AutoPostBack="true" TabIndex="0">
</asp:DropDownList>
</td>
<td class="style4">
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ControlToValidate="drp_edition" ErrorMessage="*" ForeColor="Red"
ValidationGroup="a"></asp:RequiredFieldValidator>
</td>
CS Code for filling of dropdown
protected void editiondrpfill_TextChanged(object sender, EventArgs e)
{
sql = "select bt.booktwo_id,bt.edition,b.bookname from library_book b , library_booktwo bt where bookname ='" + txt_bookname.Text+ "' and bt.book_id=b.book_id";
ds = obj.openDataset(sql, Session["SCHOOLCODE"].ToString());
drp_edition.Items.Clear();
ListItem li = new ListItem();
li.Text = "Select the value";
li.Value = "0";
drp_edition.Items.Add(li);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
li = new ListItem();
li.Text= ds.Tables[0].Rows[i]["edition"].ToString();
li.Value = ds.Tables[0].Rows[i]["booktwo_id"].ToString();
drp_edition.Items.Add(li);
}
}
cs code for reset button event
protected void btn_reset_Click(object sender, EventArgs e)
{
txt_bookname.Text = "";
txt_condition.Text = "";
txt_member_id.Text = "";
txt_nameofstudent.Text = "";
txt_quantity.Text = "";
drp_edition.SelectedIndex = 0;
drp_isbn.SelectedIndex = 0;
}
Do one thing on your aspx.cs page add a ListItem to dropdown with value 0 like
<asp:DropDownList ID="drp_edition" runat="server" ValidationGroup="a"
Width="120px" onselectedindexchanged="drp_edition_SelectedIndexChanged"
AutoPostBack="true" TabIndex="0">
<asp:ListItem Value="0" Text="Select" ></asp:ListItem>
</asp:DropDownList>
It will solve your problem.
Hope it works.
Place the default list item for the dropdown "drp_edition".
Since,you are assigned the selectedindex=0 in reset fn.
There should some list item in this case.
<td class="style4">
<asp:DropDownList ID="drp_edition" runat="server" ValidationGroup="a" Width="120px"
onselectedindexchanged="drp_edition_SelectedIndexChanged" AutoPostBack="true" TabIndex="0">
<asp:ListItem Text="--Select--" Value="0" />
</asp:DropDownList>
</td>
<td class="style4">
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ControlToValidate="drp_edition" ErrorMessage="*" ForeColor="Red" InitialValue="0"
ValidationGroup="a"></asp:RequiredFieldValidator>
</td>

listview persistent insert value

"I have a ListView with items to edit and an insert.
The insert item template has several text boxes however I would like to make one of the textboxes readonly and keep a value in it that will persist after every insert.
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
</td>
<td>
<asp:TextBox ID="tc_dateTextBox" runat="server" Text='<%# Bind("tc_date") %>' />
</td>
<td>
<asp:TextBox ID="tc_costTextBox" runat="server" Text='<%# Bind("tc_cost") %>' />
</td>
<td>
<asp:TextBox ID="tc_typeTextBox" runat="server" Text='<%# Bind("tc_type") %>' />
</td>
<td>
<asp:TextBox ID="tc_commentTextBox" runat="server" Text='<%# Bind("tc_comment") %>' />
</td>
<td> </td>
<td>
<asp:TextBox ID="tc_t_idTextBox" runat="server" Text='<%# Bind("tc_t_id") %>' Width="15" ReadOnly="true" />
</td>
</tr>
</InsertItemTemplate>
tc_t_idTextBox is the one that I made readonly and would like that to be the box that keeps the same value on every insert.
In order to make a TextBox readonly you will need to set the ReadOnly=true
example:
<asp:TextBox ReadOnly="True"...
I finally figured out how to fix my issue.
My work around was to add a hidden field to the page.
<asp:HiddenField ID="h_tc_t_id_holder" Value="0" runat="server" />
in code behind I set the value to the hidden field.
h_tc_t_id_holder.Value = App_id;
In the list view i add
OnPreRender="ListView1_OnPreRender"
And in codebehind
protected void ListView1_OnPreRender(object sender, EventArgs e)
{
ListView1.DataBind();
((TextBox)ListView1.InsertItem.FindControl("tc_t_idTextBox")).Text = h_tc_t_id_holder.Value;
}

Keeping prior values when using listview insert

Currently I have a gridview that when I click on select it populates values in a listview.
I handle this in code behind using the selectedindexchanged event. And I even populate a text box in the insertitem template for new entries.
protected void GridView9_SelectedIndexChanged(object sender, EventArgs e)
{
// this handles the transmittal costs
SqlDataSource29.SelectParameters.Clear();
SqlDataSource29.SelectParameters.Add("tc_t_id", App_id);
SqlDataSource29.InsertParameters.Clear();
ListView1.Visible = true;
ListView1.DataBind();
((TextBox)ListView1.InsertItem.FindControl("tc_t_idTextBox")).Text = App_id;
However the issue arises when I do an insert. I lose value I put into the listviews insertitem texbox tc_t_idTextBox. Actually I lose the value when I edit and delete also.
There must be a way to hold onto that value between inserts.
<InsertItemTemplate>
<tr style="">
<td>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert"
Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
Text="Clear" />
</td>
<td>
<asp:TextBox ID="tc_dateTextBox" runat="server" Text='<%# Bind("tc_date") %>' />
</td>
<td>
<asp:TextBox ID="tc_costTextBox" runat="server" Text='<%# Bind("tc_cost") %>' />
</td>
<td>
<asp:TextBox ID="tc_typeTextBox" runat="server" Text='<%# Bind("tc_type") %>' />
</td>
<td>
<asp:TextBox ID="tc_commentTextBox" runat="server" Text='<%# Bind("tc_comment") %>' />
</td>
<td>
</td>
<td>
<asp:TextBox ID="tc_t_idTextBox" runat="server"
Text='<%# Bind("tc_t_id") %>' Enabled="false" Width="15" />
</td>
</tr>
</InsertItemTemplate>
By using DataBind() on every PostBack, the ListView is being reloaded every time the user performs and action. In the Page_Load of your page, do this instead:
SqlDataSource29.SelectParameters.Clear();
SqlDataSource29.SelectParameters.Add("tc_t_id", App_id);
SqlDataSource29.InsertParameters.Clear();
ListView1.Visible = true;
if(!IsPostBack)
{
ListView1.DataBind();
}
((TextBox)ListView1.InsertItem.FindControl("tc_t_idTextBox")).Text = App_id;

Categories

Resources