How can i display a navigation property in a gridview control? - asp.net

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.

Related

Refresh Captcha Code with Updatepanel control in ASP>NET

I have a captcha code in my site.
sometimes needs to chenge that picture , but i dont want the whole page refreh.
i used updatepanel in this way :
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblmsg" runat="server" Font-Bold="True"
ForeColor="Red" Text=""></asp:Label>
<asp:TextBox ID="txtimgcode" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl="~/CImage.aspx" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Check" />
<asp:Button ID="Button3" runat="server" Text="refresh" OnClick="Button3_Click" />
</ContentTemplate>
</asp:UpdatePanel>
Here is Button3 code behind :
protected void Button3_Click(object sender, EventArgs e)
{
Image1.ImageUrl = "~/CImage.aspx";
}
Button1 Works correctly But Button3 which should change the captcha picture doesn't work.
Am I missing something?
In addition if Iwant to use a refresh image instead of Button3,What controls should I use?
The image likely isn't updating during partial postback because the browser doesn't realize that the image content has changed since it been targeted to same page over and over. Adding querystring might help in this case one need to pass random id as querystring to the ImageUrl of the captcha.
protected void Button3_Click(object sender, EventArgs e)
{
Image1.ImageUrl = string.Format("~/CImage.aspx?img={0}", Guid.NewGuid());
}
There is nothing wrong with your code Kindly check if "~/CImage.aspx" is returning correct values or not. I have modified your code Kindly check:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Label ID="lblmsg" runat="server" Font-Bold="True"
ForeColor="Red" Text=""></asp:Label>
<asp:TextBox ID="txtimgcode" runat="server"></asp:TextBox>
<asp:Image Width="100" Height="100" ID="Image1" runat="server" ImageUrl="~/CImage.aspx" />
<asp:Button ID="Button1" runat="server" Text="Check" />
<asp:Button ID="Button3" OnClick="Button3_Click" runat="server" Text="refresh" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button3" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
protected void Button3_Click(object sender, EventArgs e)
{
if (Image1.ImageUrl == "Koala.jpg")
{
Image1.ImageUrl = "Close.png";
}
else
{
Image1.ImageUrl = "Koala.jpg";
}
}

update panel does not work

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 :)

error on dynamic combobox asp.net when binding data

related to this post Fill ComboBox Dynamically in asp.net.. help required I follow the code from that post, but when I tried to binding data, I've got error
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
<asp:TemplateField HeaderText="Step" SortExpression="ID"><InsertItemTemplate>
: <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource3" AutoPostBack="true"
DataTextField="Name" DataValueField="code" SelectedValue='<%# Bind("ID") %>' AppendDataBoundItems="true"
Width="300px" Height="25px">
<asp:ListItem Value=" " Text="---Select---"></asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="KKL Step" SortExpression="ID">
<InsertItemTemplate>
: <asp:DropDownList ID="DropDownList6s" runat="server" DataSourceID="SqlDataSource5"
DataTextField="KKL_Name" DataValueField="ID" AppendDataBoundItems="true"
SelectedValue='<%# Bind("ID") %>' Width="300px" Height="25px">
<asp:ListItem Value="" Text="---Select---"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource5" runat="server"
ConnectionString="<%$ ConnectionStrings:Koneksi %>"
SelectCommand="SELECT * FROM [TBL_KKL] WHERE (code=#code)">
<SelectParameters>
<asp:controlParameter controlID="DropDownList2"
propertyname="SelectedValue" Name="code" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</InsertItemTemplate>
</asp:TemplateField>
Its better to bind values to dropdown lists using C# code. Its ease and clear.
So in the RowDataBound event of the gridview you can write a code similar to this according to your syntax.
protected void dtgItemDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dtDetails = GetDetailsFromDataBase();
DropDownList DropDownList2= (DropDownList)e.Row.FindControl("DropDownList2");
DropDownList2.DataTextField = "Name";
DropDownList2.DataValueField = "ID";
DropDownList2.DataSource = dtDetails ;
DropDownList2.DataBind();
}
}
This is a sample code and you should change it according to yours.
Instead of DB calls inside the RowDataBound method, you can have Sessions or ViewStates with already filled with your data.

how to get value from dropdownlist which in ModalPopupExtender

I have a modalpopup which is showed automaticaly on startup of page. I'm doing this on Page_Load event.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
InitiallizeAll();
}
}
private void InitiallizeAll()
{
ModalPopupExtender1.Show();
}
In aspx side I have a dropdownlist on a modalpopup but the index is always posting zero to the javascript code.
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
TargetControlID="lnkPopup"
PopupControlID="panEdit"
BackgroundCssClass="modalBackground"
OkControlID="btnGonder"
OnOkScript="onOk()">
</asp:ModalPopupExtender>
<asp:Panel ID="panEdit" runat="server" Height="180px" Width="400px" CssClass="modalPopup">
<table width="100%">
<tr>
<td class="labelCell">
<asp:Label ID="lblBolge" Text="Bölge" runat="server" />
</td>
<td>
<asp:DropDownList ID="ddlIl" runat="server"
AutoPostBack="False"
DataTextField="Isim"
DataValueField="Id"
DataSourceID="ObjectDataSource1"
Width="176px">
</asp:DropDownList>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
SelectMethod="GetAllIl"
TypeName="ERK.Lasos.Business.CustomEntities.Il">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="0"
Name="ilId"
QueryStringField="bolge"
Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
</td>
</tr>
</table>
<br />
<asp:Button ID="btnGonder" runat="server" Text="Devam Et" />
</asp:Panel>
<a id="lnkPopup" runat="server">Show Popup</a>
And this is my javascript code
<script type="text/javascript">
function onOk() {
var e = document.getElementById("ddlIl");
var selectedIlId = e.options[e.selectedIndex].value;
window.location = "/Pages/AnaSayfa/LasosAnaSayfa.aspx?bolge=" + selectedIlId;
}
</script>
But I'm always getting selectedIlId is "0". Value is not changing whatever I select from dropdownlist
First, you should check if you got the good selected index. :
Replace temporally var selectedIlId = e.options[e.selectedIndex].value; by var selectedIlId = e.selectedIndex;
If it's OK, you should check you data source by making sure the Id field is correct, the easiest way to to this is to set DataTextField="Id" to your DropdownList and check the values that appears in it.

asp EntityDataSource detailsview OnItemUpdating e.cancel

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!

Resources