I need to use DataList inside another DataList - c#

<asp:DataList ID="DataListDziennik" runat="server"
DataSourceID="SqlDataSourcePrzedmioty">
<ItemTemplate>
<asp:Label ID="LabelPrzedmiot" runat="server" Text='<%# Eval("przedmiot") %>' />
...
<asp:DataList ID="DataListOceny" runat="server"
DataSourceID="SqlDataSourceOceny"
RepeatDirection="Horizontal"
OnItemCommand="DataListOceny_ItemCommandOceny"
OnEditCommand="DataListOceny_EditCommandOceny">
<EditItemTemplate>
<asp:TextBox ID="TextBoxOcena" runat="server" Text='<%# Bind("lista") %>' />
<td><asp:Button ID="ButtonZapisz" CommandName="Update" runat="server" Text="Zapisz" /></td>
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox Width="20" ID="TextBoxOcena" ReadOnly="true" Text='<%# Eval("lista") %>' runat="server"></asp:TextBox>
<td><asp:Button ID="ButtonEdytuj" CommandName="Edit" runat="server" Text="Edytuj" /></td>
</ItemTemplate>
</asp:DataList>
</td>
</ItemTemplate>
When I write this in code behind:
protected void DataListOceny_EditCommand(object source, DataListCommandEventArgs e)
{
DataListOceny.EditItemIndex = e.Item.ItemIndex;
DataListOceny.DataBind();
}
Visual Studio tells me that DataListOceny does not exist in current content. I just want to be able edit items on DataListOceny after clicking the "edit" button, please give me full code in code behind part...
for edit DataListOceny .

What you asking is same like the below thread.. You need to find the DataListOceny Control on each row of DataListDziennik to bind it
Using FindControl() to find control
https://msdn.microsoft.com/en-us/library/486wc64h%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
And Here is some good example on the same
http://www.c-sharpcorner.com/UploadFile/rohatash/nested-datalist-in-Asp-Net/
http://surecode.net/DevCentre/NestedDataList/NestedDataList.aspx

Related

ASP.Net FormView won't update with Response.Redirect code

I have a fairly simple FormView control I'm using with ASP.Net/C#. My issue is when I use the code to redirect after the update/edit, the FormView won't actually perform the update, but, it will Redirect. I won't post all of the FormView code because it does update when I comment out the Redirect code. I think what I am asking is how to change the code to update while using the Redirect Code? Again, the update works fine when I don't do the Redirect! Is it possible the redirect is happening before the update can take place?
Code for Redirect
<script runat="server">
protected void FormView1_ItemUpdating(Object sender, FormViewUpdateEventArgs e)
{
{
Response.Redirect("redirect_main.aspx");
}
}
</script>
FormView1
<asp:FormView ID="FormView1" runat="server" onitemupdating="FormView1_ItemUpdating" DataKeyNames="req_submitted_key" DataSourceID="SqlDataSource2" DefaultMode="Edit" >
<EditItemTemplate>
req_submitted_key:
<asp:Label ID="req_submitted_keyLabel1" runat="server" Text='<%# Eval("req_submitted_key") %>' />
<br />
Role_job_title:
<asp:TextBox ID="Role_job_titleTextBox" runat="server" Text='<%# Bind("Role_job_title") %>' />
<br />
requestname:
<asp:TextBox ID="requestnameTextBox" runat="server" Text='<%# Bind("requestname") %>' />
<br />
submitted_by_name:
<asp:TextBox ID="submitted_by_nameTextBox" runat="server" Text='<%# Bind("submitted_by_name") %>' />
<br />
Submitted_by_email:
<asp:TextBox ID="Submitted_by_emailTextBox" runat="server" Text='<%# Bind("Submitted_by_email") %>' />
<br />
submitted_date:
<asp:TextBox ID="submitted_dateTextBox" runat="server" Text='<%# Bind("submitted_date") %>' />
<br />
submitted_by_comment:
<asp:TextBox ID="submitted_by_commentTextBox" runat="server" Text='<%# Bind("submitted_by_comment") %>' />
<br />
approved_denied:
<asp:TextBox ID="approved_deniedTextBox" runat="server" Text='<%# Bind("approved_denied") %>' />
<br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel" />
</EditItemTemplate>
Yes...it redirecting before the update. Take your redirect out of ItemUpdating and put it in ItemUpdated.
From: DetailsView.ItemUpdatint Event
DetailsView.ItemUpdating Event
Occurs when an Update button within a DetailsView control is clicked,
but before the update operation.
DetailsView.ItemUpdated Event
Occurs when an Update button within a DetailsView control is clicked,
but after the update operation.
DetailsView.ItemUpdated Event
Your code should look like:
protected void FormView1_ItemUpdated(Object sender, FormViewUpdateEventArgs e)
{
Response.Redirect("redirect_main.aspx");
}

how to visible false edit commandbutton inside Gridview

I am working on an asp.net application. In which i have added a commandbutton which is used to edit the row. My Gridview is
<asp:GridView ID="gvCustomerPaymentDetails" runat="server"
AutoGenerateColumns="false" ShowFooter="true" OnRowEditing="EditPayment"
OnRowUpdating="UpdatePayment" OnRowCancelingEdit="CancelEdit"
CssClass="table table-bordered table-hover" Style="margin-left: 5px; margin-right: 5px;">
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Eval("ID")%>'></asp:Label>
</ItemTemplate>
<ItemStyle Width="2%" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Pay Amount">
<ItemTemplate>
<asp:Label ID="lblPayAmount" runat="server" Text='<%# Eval("Pay_Amount")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPayAmount" runat="server" style="width:100%" Text='<%# Eval("Pay_Amount")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPayAmount" style="width:100%" runat="server"></asp:TextBox> </FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:LinkButton ID="linkPayNow" runat="server" Text="Pay Now" CommandArgument='<%#Eval("ID") %>' CommandName="Pay"></asp:LinkButton>
<asp:Label ID="txtStatus" runat="server" Text="Paid" Style="margin-left: 20px;" Visible="false"></asp:Label>
<asp:LinkButton ID="lnkRemove" runat="server" CommandArgument='<%# Eval("ID")%>' OnClientClick="return confirm('Do you want to delete?')"
Text="Delete" OnClick="DeletePayment"></asp:LinkButton>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="AddNewPayment" CommandName="Footer" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
<EmptyDataTemplate>
<tr>
<th scope="col">Pay Amount
</th>
<th scope="col"></th>
</tr>
<tr>
<td>
<asp:TextBox ID="txtPayAmount" runat="server" />
</td>
<td>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="AddNewPayment" CommandName="EmptyDataTemplate" />
</td>
</tr>
</EmptyDataTemplate>
</asp:GridView>
I have a Label "lblStatus" which will be either 0 or 1. If its value is 1, then i want to visible false the Update and edit commandbutton of that specific row. I have searched a lot but not found any perfect solution. Please help me someone.
In this case, I would convert the CommandField to TemplateFields. To do so, you will click on the smart tag in the gridview in design view. What this will do is change the commandfields to linkbuttons, where you can set it to false in the behind code. You will want to give your new linkbuttons proper ID's so you can find them in the behind code. Then you will create a databound method.. like..
protected void GridView1_OnRowDataBound(object sender, GridViewRowEventArgs e)
{
//Code Here to Disable button. I'd use a Foreach loop like this.
foreach(GridViewRow gvr in GridView1.Rows)
{
Label label = ((Label)gvr.FindControl("label"));
LinkButton edit = ((LinkButton)gvr.FindControl("edit"));
if (label.Text == 1)
{
edit.Visible = false;
}
}
}
Hopefully this puts you in the right direction. I think this should do the trick.

get object from listview after clicking a button

I'm trying to get the original object back from a listview, when I click on the button.
I thought it was in e.Item.DataItem but that always seems to be null.
<form runat="server">
<asp:ListView ID="ListList" runat="server">
<ItemTemplate>
<asp:TextBox ID="tbCompanyName" runat="server" Text='<%# Eval("CompanyName") %>'></asp:TextBox>
<asp:TextBox ID="tbEmailAdress" runat="server" Text='<%# Eval("EmailAddres") %>'></asp:TextBox>
<asp:DropDownList ID="ddlAccountManagers" AutoPostBack="True" runat="server" />
<asp:Button runat="server" Text="Create or Update Account" CommandArgument='<%# Container.DataItem.ToString() %>' />
<br />
</ItemTemplate>
</asp:ListView>
</form>
private void ListList_ItemCommand(object sender, ListViewCommandEventArgs e)
{
}
You need to keep the original datasource in session and get the real values while clicking a button.

I can't get the value of textbox from the footer row of gridview

i can't get the value of textbox from the footer row of gridview
<asp:GridView ID="GridView1" runat="server" Width="1214px"
AutoGenerateColumns="False" ShowFooter="true"
OnRowCommand="GridView1_RowCommand"
<Columns>
<asp:TemplateField HeaderText="Insert">
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name">
<EditItemTemplate>
<asp:Label ID="lblEditSName" runat="server" Text='<%#Eval("sname") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSName" runat="server" Text='<%#Eval("sname") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtSName" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and the code behind is........
i can't get the value of textbox from the footer row of gridview
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName==("AddNew"))
{
TextBox txtName =(TextBox) GridView1.FooterRow.FindControl("txtSName");
string strName=txtName.Text; //strName is Empty while i m enters data into the textbox txtSName
}
Posting this comment to help others who may encounter the same issue.
Make sure the grid is not loading again on post back.
code in the page load should look like this
if (!IsPostBack)
{
LoadGrid();
}
Your mark up is biting you.You are having two footer templates within a template field.
Do this
<asp:TemplateField HeaderText="Insert">
<ItemTemplate>
<asp:ImageButton ID="EditImageButton" runat="server" CommandName="Edit"
ImageUrl="~/images/Edit.png"Style="height: 16px" ToolTip="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="btnDelete" runat="server" CommandName="Delete"
CausesValidation="false" OnClientClick="return confirm('Delete.Are you sure you want to delete?')"
ImageUrl="~/images/DeleteTS.png" Text="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="AddNewImgBtn" runat="server" ImageUrl="~/images/saveHS.png"
ToolTip="Add New" AlternateText="Add New" Width="16px" Height="16px"
CommandArgument="InsertNew" ImageAlign="AbsMiddle" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Student Name">
<EditItemTemplate>
<asp:Label ID="lblEditSName" runat="server" Text='<%#Eval("sname") %>' />
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblSName" runat="server" Text='<%#Eval("sname") %>' />
</ItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtSName" runat="server"/>
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And then you can gracefully locate your footer row.
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandArgument=="InsertNew")
{
GridView testGrid=(Gridview)sender;
TextBox txtName =(TextBox)testGrid.FooterRow.FindControl("txtSName");
string strName=txtName.Text;
}
}
See working example Example

Access EditItemTemplate from CodeBehind

I have problem dealing with EditItemTemplate.
What I am trying to do is to update my TextBox txt_name but I can't get what the user ingresses And I get the old value instead at the code-behind.
Am I missing something?
FRONT CODE
<asp:GridView ID="GridView_account" runat="server" AutoGenerateColumns="false" ShowFooter="True"
OnRowCancelingEdit="GridView_account_RowCancelingEdit" OnRowEditing="GridView_account_RowEditing"
OnRowUpdating="GridView_account_RowUpdating" OnRowCommand="GridView_account_RowCommand"
OnRowDeleting="GridView_account_RowDeleting" OnSelectedIndexChanged="GridView_account_SelectedIndexChanged"
DataKeyNames="UID" Height="110px" Width="497px">
<Columns>
<asp:TemplateField HeaderText="UID" SortExpression="UID">
<ItemTemplate>
<asp:Label ID="label_accid" runat="server" Text='<%# Bind("UID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="User Name (required)" SortExpression="NyA">
<EditItemTemplate>
<asp:TextBox ID="txt_name" runat="server" Text='<%# Bind("NyA") %>'/>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txt_newname" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="label_name" runat="server" Text='<%# Bind("NyA") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Email Address (required)" SortExpression="eMail">
<EditItemTemplate>
<asp:TextBox ID="txt_email" runat="server" Text='<%# Bind("eMail") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txt_newemail" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="label_email" runat="server" Text='<%# Bind("eMail") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Options" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton_update" runat="server" CausesValidation="True" CommandName="Update"
Text="Update"></asp:LinkButton>
<asp:LinkButton ID="LinkButton_cancel" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel"></asp:LinkButton>
<asp:LinkButton ID="LinkButton_delete" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="LinkButton_addnew" runat="server" CausesValidation="False" CommandName="AddNew"
Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton_edit" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Manage Role" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="Selectbtn" runat="server" CausesValidation="False" CommandName="Select"
Text="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
CODE BEHIND
protected void GridView_account_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
user u = Business.GetUser(((Label)GridView_account.Rows[e.RowIndex].FindControl("label_accid")).Text);
//HERE'S MY PROBLEM!
u.fullname = ((TextBox)GridView_account.Rows[e.RowIndex].FindControl("txt_name")).Text;
//txt_name is returning the old value and not the one the user has input recently.
Business.UpdateUser(u);
GridView_account.EditIndex = -1;
fillgridview();
}//
Are you checking if Page IsPostback when binding your grid? You need to not rebind it in the Page_Load if it is a post back, or else you will lose the new values, because the Page Init, Page Load events happen before other events, such as a Grid Updating event.
May be you are binding you grid on every page_load event. If so, you need something like this:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
GridView_account.DataSource = "data source";
GridView_account.DataBind();
}
}
or instead of
if(!Page.IsPostBack)
you can use
if (GridView_account.EditIndex == -1)
this checks, if grid is in edit mode. If grid is not in edit mode, you can bind your grid with data source.

Categories

Resources