I will explain my problem.
So I have an asp page with 2 entityDataSource.
- The first one is used to fill my obout table:
<asp:EntityDataSource ID="EntityDataSource1" ContextTypeName="RichDataBusinessApplication.Web.RichDataEntities" EnableFlattening="false" EnableInsert="true"
EnableUpdate="true" EnableDelete="true" EntitySetName="UserInformations" runat="server" />
My obout table is in an updatepanel. When I want to edit a row in my table, this method is handled:
protected void Button_Command(object sender, CommandEventArgs e)
{
SelectedUserHiddenField.Value = e.CommandArgument.ToString();
UsersCustomerDetailsView.ChangeMode(DetailsViewMode.Edit);
OpenPopupDialog(String.Format("#{0}", UpdatePopupPanel.ClientID));
}
The function OpenPopupDialog open a jquery popup dialog with the informations of my user I can now edit:
<asp:Panel ID="UpdatePopupPanel" runat="server" CssClass="popup-dialog">
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="UsersGrid" EventName="Select" />
</Triggers>
<ContentTemplate>
<asp:DetailsView runat="server" ID="UsersCustomerDetailsView" DataSourceID="SelectedUserDataSource2" DefaultMode="Edit" AutoGenerateEditButton="true" AutoGenerateRows="false"
OnItemUpdating="UserDetailsView_ItemUpdating" OnItemUpdated="UserDetailsView_ItemUpdated"
OnItemCommand="UserDetailsView_ItemCommand"
DataKeyNames="UserId" Width="100%">
<Fields>
<asp:TemplateField>
<HeaderTemplate>
<uc:CulturalLabel ID="CulturalLabel1" runat="server" Text="Lastname" />
</HeaderTemplate>
<EditItemTemplate>
<asp:TextBox ID="LNameTextBox" runat="server" Text='<%# Bind("Lastname") %>' CssClass="textEntry" ValidationGroup="CoordonneValidationGroupUp"/>
<asp:RequiredFieldValidator ID="CustomRequieredValidator2" runat="server" ControlToValidate="LNameTextBox" CssClass="failureNotification" ErrorMessage="test" ValidationGroup="CoordonneValidationGroupUp">*</asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Panel>
The second EntityDataSource allow to fill the fields of my popup of my detailsview is this one:
<asp:EntityDataSource ID="SelectedUserDataSource2" ContextTypeName="RichDataBusinessApplication.Web.RichDataEntities" EnableFlattening="false" EnableInsert="true" EnableUpdate="true" EnableDelete="true" EntitySetName="UserInformations" runat="server" AutoGenerateWhereClause="True">
<WhereParameters>
<asp:ControlParameter Name="UserId" ControlID="SelectedUserHiddenField" DbType="Guid" PropertyName="value" />
</WhereParameters>
</asp:EntityDataSource>
When I clic on the update button of my popup I handled the "OnItemUpdating" of my detailsview:
protected void UserDetailsView_ItemUpdating(Object sender, DetailsViewUpdateEventArgs e){
Page.Validate("CoordonneValidationGroupUp");
if (Page.IsValid)
{
//code to execute if page is valid
}
else
{
e.cancel = true;
}
}
The problem is the following:
When my page is not valid because of the lastname is blank, "e.cancel=true" is executed but the "*" and error message don't appear and the field of my popup is reloaded with the data of the first row in my database.
So, if someone can help me!
Thanks!
Related
im trying to get the value of a dropdownlist and insert it into a label inside an update panel like the following:
<asp:UpdatePanel ID="udpTutorialDropDown" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="TutorialSeries" DataTextField="SeriesName" DataValueField="VideoSeriesNameID" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList><br />
<asp:SqlDataSource ID="TutorialSeries" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="ViewSeasonName" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
<asp:Label ID="lblEpisode" runat="server" Text="Label"></asp:Label><br />
<asp:TextBox ID="tbxURL" runat="server"></asp:TextBox><br />
<asp:TextBox ID="tbxDiscription" runat="server"></asp:TextBox><br />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="DropDownList1" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
and in code behind i have
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
lblEpisode.Text = DropDownList1.SelectedValue.ToString();
}
but i dont know why it doesnt update the label!! the text of the label remains the same!!! can someone spot the problem??
You have to put drop down list into existing Update panel as well...
Try to remove the trigger to get something like so
<br />
<asp:SqlDataSource ID="TutorialSeries" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" SelectCommand="ViewSeasonName" SelectCommandType="StoredProcedure"></asp:SqlDataSource>
<asp:UpdatePanel ID="udpTutorialDropDown" runat="server">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="TutorialSeries" DataTextField="SeriesName" DataValueField="VideoSeriesNameID" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList>
<asp:Label ID="lblEpisode" runat="server" Text="Label"></asp:Label><br />
<asp:TextBox ID="tbxURL" runat="server"></asp:TextBox><br />
<asp:TextBox ID="tbxDiscription" runat="server"></asp:TextBox><br />
</ContentTemplate>
You need to call the Update() method of the update panel in your Event as shown below.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
lblEpisode.Text = DropDownList1.SelectedValue.ToString();
udpTutorialDropDown.Update();
}
All the best!
UPDATE
You must add AutoPostBack="true" property in the drop down list. And, ignore my previous instructions. I.e. calling the Update() method of the update panel. That's required only when you have UpdateMode="Conditional"
This should work :)
i use gridview and UpdatePanel in asp.net.
<asp:UpdatePanel ID="upSubject" runat="server">
<ContentTemplate>
<div class="c_box" style="height: auto; text-align: center; margin-top: 10px; width:565px; padding-right:140px;">
<asp:GridView ID="gvSubject" runat="server" AutoGenerateColumns="False"
SkinID="gvSubject" ShowHeader="true" ShowHeaderWhenEmpty="true"
onrowcommand="gvSubject_RowCommand">
<Columns>
<asp:BoundField DataField="lblCounter" HeaderText="Number" ReadOnly="True" HeaderStyle-Font-Bold="false" ItemStyle-Width="60px"></asp:BoundField>
<asp:BoundField DataField="lblSubject" HeaderText="Tiltle" ReadOnly="True" HeaderStyle-Font-Bold="false" ItemStyle-Width="300px" ItemStyle-HorizontalAlign="Right"></asp:BoundField>
<asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="false" ItemStyle-Width="60px">
<ItemTemplate>
<asp:ImageButton ID="dltSubject" runat="server" CommandName="remove" CommandArgument="lblCounter" ValidationGroup="Subject" OnClientClick="return confirm('Delete?')" ImageUrl="~/Style/Pic/pic_delete_small.png" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#000000" />
<EmptyDataRowStyle BackColor="#00ff00" />
<EmptyDataTemplate>
Not item...
</EmptyDataTemplate>
</asp:GridView>
</div>
</ContentTemplate>
<Triggers>
<asp:asyncpostbacktrigger controlid="gvSubject" eventname="RowCommand" />
</Triggers>
</asp:UpdatePanel>
or
<asp:asyncpostbacktrigger controlid="dltSubject" eventname="Click" />
.
protected void gvSubject_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "remove")
{
DataTable _dt = (DataTable)Session["Subject"];
_dt.Rows[int.Parse(e.CommandArgument.ToString())].Delete();
gvSubject.DataSource = _dt;
gvSubject.DataBind();
Session["Subject"] = _dt;
}
}
but not work UpdatePanel. please help me for work button gridview in UpdatePanel.
i want work ImageButton in gridview and UpdatePanel.
You dont have to use Triggers for the RowCommand. Also remove rowcommand if you dont want to use besides this button click.
Please specify OnCommand=dtlSubject_Click
I have used here "Number" as commandargument thinking that it is your unique id needed to delete the record.
You need to specify unique column for deletion in commandargument.
<asp:ImageButton ID="dltSubject" runat="server"
CommandArgument='<%#DataBinder.Eval(Container.DataItem, "Number")%>'
ValidationGroup="Subject" OnCommand="dtlSubject_Click"
OnClientClick="return confirm('Delete?')" ImageUrl="~/Style/Pic/pic_delete_small.png" />
And on server side write down following code :
protected void dtlSubject_Click(object sender, CommandEventArgs e)
{
string uniqueid= e.CommandArgument.ToString();
//call your procedure here to delete your item.
}
I am new to ASP.NET. I am trying to insert a item into a dropdownlist when click "New" to add new record. which the form will switch from ItemTemplate to InsertTempplate
Error:
"System.NullReferenceException: Object reference not set to an
instance of an object."
this code in my DetailsView.
<ItemTemplate>
<asp:Label ID="lblVendorName"
runat="Server" style="text-align:left; width:100%"
Text='<%# Eval("VendorName")%>' Width="70%"/>
</ItemTemplate>
<InsertItemTemplate>
<asp:DropDownList id="insertVendorName" datasourceid="VendorSqlDataSource"
datatextfield="VendorName" DataValueField="VendorID"
OnSelectedIndexChanged="ddlVendor_SelectedIndexChanged"
runat="server" AutoPostBack="true" />
<asp:SqlDataSource ID="VendorSqlDataSource"
ConnectionString="<%$Connectionstrings:ConnectionString%>"
SelectCommand="SELECT VendorID, VendorName
from MDF_Vendor" runat="server">
</asp:SqlDataSource>
</InsertItemTemplate>
Updated... Below is set of Link Bottons.
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnedit" runat="server"
CommandName="Edit" Text="Edit"/> |
<asp:LinkButton ID="btnInsert" runat="Server"
Text="New" CommandName="New"
OnClick="LinkButton_Click" /> |
<asp:LinkButton ID="btnDelete"
runat="server"
CommandName="Delete" Text="Delete" /> |
</ItemTemplate>
<InsertItemTemplate>
<asp:LinkButton ID="btnInsert" runat="Server"
Text="Insert" CommandName="Insert"/>
<asp:LinkButton ID="btncancel" runat="server"
CommandName="Cancel" Text="Cancel"/>
</InsertItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btnupdate" runat="server"
CommandName="Update" Text="Update" />
<asp:LinkButton ID="btncancel" runat="server"
CommandName="Cancel" Text="Cancel"/>
</EditItemTemplate>
</asp:TemplateField>
I tried this code below suggested by #Aghislas (Thanks!)
protected void LinkButton_Click(object sender, EventArgs e)
{
DropDownList ddlVendor = (DropDownList)DetailsView1.FindControl("insertVendorName");
ddlVendor.Items.Insert(0, new ListItem("---Select---", "-1"));
}
Please help. Thank you.
You can try with this code
<asp:LinkButton id="LinkButton1"
Text="Click Me"
OnClick="LinkButton_Click"
runat="server"/>
protected void LinkButton_Click(Object sender, EventArgs e)
{
var ddlVendor =(DropDownList)DetailsView1.FindControl("insertVendorName");
ddlVendor.Items.Insert(0, new ListItem("---Select---", "-1"));
}
Nota : You delete code in the Page_Load and move in Click delegate
I want to display a field value from a navigation property in a grid view control.
My entities are Albums and Photos and a Photo entity must be associated with an Album entity.
I want to display an albums’s name but it is never shown in the gridview control.
The following is my markup:
<h4>
<asp:DropDownList ID="ddlAlbums" runat="server" Style="padding: 3px; width: 150px;"
AutoPostBack="True">
</asp:DropDownList>
</h4>
<br />
<asp:UpdatePanel ID="pnlPhotos" runat="server" >
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlAlbums" />
</Triggers>
<ContentTemplate>
<asp:DataList ID="PhotosDataList" runat="server" DataKeyField="PhotoID" DataSourceID="PhotosEntityDataSource"
RepeatDirection="Horizontal" CssClass="view" RepeatColumns="5"
onitemdatabound="PhotosDataList_ItemDataBound">
<ItemTemplate>
<asp:Image ID="img" CssClass="thumbnail" ToolTip='<%#Eval("Title")%>' runat="server"
ImageUrl='<%#Eval("Caption","~/Uploads/Pictures/{0}")%>' />
<br />
**<asp:Label ID="lblAlbumName" runat="server" Text='<%#Eval("Album.Name")%>'></asp:Label>**
<asp:LinkButton ID="lnkEdit" runat="server" CommandName="Select" CommandArgument='<%#Eval("PhotoID")%>'
OnClick="lnkEdit_Click">edit</asp:LinkButton>
<asp:LinkButton ID="lnkDelete" runat="server" CommandArgument='<%#Eval("PhotoID")%>'
OnClick="DelPhoto_Click">delete</asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
<asp:EntityDataSource ID="PhotosEntityDataSource" runat="server" ContextTypeName="Misagh.DAL.MisaghSchoolEntities"
EnableDelete="True" EntitySetName="Photos" Where="it.AlbumID = #AlbumID">
<WhereParameters>
<asp:ControlParameter ControlID="ddlAlbums" DefaultValue="0" DbType="Int32" PropertyName="SelectedValue"
Name="AlbumID" />
</WhereParameters>
</asp:EntityDataSource>
I would really appriciated any help
Thanks
You already handle OnItemDataBound. You can simply do:
protected void PhotosDataList_ItemDataBound(Object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item ||
e.Item.ItemType == ListItemType.AlternatingItem)
{
// Retrieve the Label control in the current DataListItem.
Label albumName = (Label)e.Item.FindControl("lblAlbumName");
albumName.Text = (e.Item.DataItem as ICustomTypeDescriptor).GetPropertyOwner(null).Album.Name;
}
}
And set EnableFlattening="true" in your EntityDataSource declaration in your markup
Read more about a related topic here.
I was trying to update the content of a modal dialog, and this code works for me:
<asp:LinkButton ID="updateSomething" runat="server" Text="Update" CausesValidation="false" OnClientClick="openDialog();" onclick="UpdateButton_Click" />
<asp:UpdatePanel ID="upNewUpdatePanel" runat="server">
<ContentTemplate>
<asp:Label ID="updateLabel" runat="server"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="updateSomething" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
However, when I try to place the LinkButton inside a gridview, like so:
<asp:GridView ID="grdListUsers" runat="server" AutoGenerateColumns="false" AllowPaging="false" OnRowDataBound="grdRowDefListUsers" CssClass="mGrid" EmptyDataText="No users.">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Nome" HeaderStyle-Width="300" />
<asp:BoundField DataField="Login" HeaderText="Login" HeaderStyle-Width="300" />
<asp:TemplateField HeaderText="Options" HeaderStyle-Width="75" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:LinkButton ID="updateSomething" runat="server" Text="Update" CausesValidation="false" OnClientClick="openDialog();" onclick="UpdateButton_Click" />
</asp:TemplateField>
</Columns>
</asp:GridView>
This does not work, I get an error saying: A control with ID 'updateSomething' could not be found for the trigger in UpdatePanel 'upNewUpdatePanel'.
How can I use the ImageButton inside the gridview?
Try and add the asp:AsyncPostBackTrigger to the asp:GridView's OnRowCommand event and handle the link button click in that event
<asp:GridView ID="grdListUsers" runat="server" onRowCommand="grdListUsers_RowCommand">
<asp:TemplateField>
<asp:LinkButton ID="updateSomething" CommandName="update-something" CommandArgument='<%# DataBinder.Eval(Container, "RowIndex") %>'/>
</asp:TemplateField>
</asp:GridView>
and in the cs create the event like this
protected void grdListUsers_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "update-something")
{
grdListUsers.SelectedIndex = Convert.ToInt32(e.CommandArgument);
}
}
You could add a trigger of the gridview
<Triggers>
<asp:PostBackTrigger ControlID="gridview1" />
</Triggers>
Add another Update Panel surrounding your link button just like below.
<asp:GridView ID="grdListUsers" runat="server" AutoGenerateColumns="false" AllowPaging="false" OnRowDataBound="grdRowDefListUsers" CssClass="mGrid" EmptyDataText="No users.">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Nome" HeaderStyle-Width="300" />
<asp:BoundField DataField="Login" HeaderText="Login" HeaderStyle-Width="300" />
<asp:TemplateField HeaderText="Options" HeaderStyle-Width="75" ItemStyle-HorizontalAlign="Center" ItemStyle-VerticalAlign="Middle">
<ItemTemplate>
<asp:UpdatePanel ID="aa" runat="server">
<ContentTemplate>
<asp:LinkButton ID="updateSomething" runat="server" Text="Update" CausesValidation="false" OnClientClick="openDialog();" onclick="UpdateButton_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="updateSomething"/>
</Triggers>
</asp:UpdatePanel>
</asp:TemplateField>
</Columns>
</asp:GridView>
You could set the UpdatePanel's UpdateMode to Conditional and update it manually from the UpdateButton_Click-Handler:
<asp:UpdatePanel ID="UdpFormPanel" runat="server" UpdateMode="conditional" ChildrenAsTriggers="false" >
LinkButton's Click-Event handler:
Protected Sub UpdateButton_Click(ByVal sender As Object, ByVal e As EventArgs)
'blah....
upNewUpdatePanel.Update()
End Sub