简体   繁体   English

当用户在asp.net的dropdownlist中选择一个值时,如何在图像控制中显示哪个图像路径存储在数据库中的图像?

[英]how to show an image in image control of which image path is stored in database when user selects a value in dropdownlist in asp.net?

I have a database which is shown below in the image : Image of database 我有一个数据库,如下图所示: 数据库的图像

Here is the image of the form : Image of the Form In this form i want to show up the image in image control of which image path is stored up in database and according to subcategory value from the drop down list when user selects it how can i do it please help ... Thank You Guys here is the source code of my form i have added sqldatasource and bind the dropdown list with sqldatasource ......... 这是表单的图像:表单的图像在此表单中,我想在图像控件中显示图像,该图像控件的图像路径存储在数据库中,并且当用户选择它时如何根据下拉列表中的子类别值显示我做到了,请帮助...谢谢你们,这是我表格的源代码,我已经添加了sqldatasource并将下拉列表与sqldatasource绑定在一起.........

<form method="post" enctype="multipart/form-data">
<table border="0" width="450px" height="500px" align="center" class="tableshadow">
    <tr>
        <td colspan="2" class="toptd" style="color: white; font-size: 24px; text-align: center;
            height: 40px; background-color: #60b2e7">
            Update Subcategory
        </td>
    </tr>
    <tr>
        <td class="lefttxt">
            <asp:Label ID="Label1" runat="server" Text="Select Subcategory:"></asp:Label>
        </td>
        <td>
            &nbsp;
            <asp:DropDownList ID="DropDownList1" runat="server" Height="35px" Width="134px" DataSourceID="SqlDataSource1"
                DataTextField="subcatname" DataValueField="subcatid">
                <asp:ListItem Text="Select category" Value="0"></asp:ListItem>
            </asp:DropDownList>
            <asp:Button ID="btnshow" runat="server" OnClick="btnshow_Click" Text="Show" Width="94px" />
        </td>
    </tr>
    <tr>
        <td class="lefttxt">
            <asp:Label ID="Label2" runat="server" Text="Subcategory Name:"></asp:Label>
            <br />
        </td>
        <td>
            <asp:TextBox ID="txtsubcategoryname" runat="server"></asp:TextBox>
            <br />
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtsubcategoryname"
                ErrorMessage="*Please Enter the Subcategory Name" Font-Bold="True" Font-Italic="True"
                Font-Size="X-Small" ForeColor="Red"></asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="lefttxt">
            <asp:Label ID="Label3" runat="server" Text="Select Category:"></asp:Label>
        </td>
        <td>
            <asp:DropDownList ID="DLCategory" runat="server" Height="35px" Width="146px" DataSourceID="SqlDataSource1"
                DataTextField="categoryname" DataValueField="categoryname">
                <asp:ListItem Text="Select category" Value="0"></asp:ListItem>
            </asp:DropDownList>
            &nbsp;
            <tr>
                <td class="lefttxt">
                    <asp:Label ID="Label6" runat="server" Text="Category Name:"></asp:Label>
                    <br />
                </td>
                <td>
                    <asp:TextBox ID="txtcategoryname" runat="server"></asp:TextBox>
                    <br />
                    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtcategoryname"
                        ErrorMessage="*Please Enter the Category Name" Font-Bold="True" Font-Italic="True"
                        Font-Size="X-Small" ForeColor="Red"></asp:RequiredFieldValidator>
                </td>
            </tr>
            <tr>
                <td class="lefttxt">
                    <asp:Label ID="Label4" runat="server" Text="Old Pic:"></asp:Label>
                </td>
                <td>
                    &nbsp;<input type="hidden" name="h1" value="" /><asp:Image ID="Image1" runat="server" />
                </td>
            </tr>
            <tr>
                <td class="lefttxt">
                    <asp:Label ID="Label5" runat="server" Text="Upload New Pic:"></asp:Label>
                </td>
                <td>
                    <asp:FileUpload ID="FileUpload1" runat="server" Width="225px" />
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;
                </td>
                <td>
                    <asp:Button ID="btnupdate" runat="server" Text="Update" OnClick="btnupdate_Click" />
                </td>
            </tr>
</table>
</form>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ToursandTravelsConnectionString %>"
    SelectCommand="SELECT [subcatid], [subcatname], [categoryname], [pic] FROM [subcategory]">
</asp:SqlDataSource>

Providing the 'pic' column in database holds links to the images are absolute, it also depends on how you have set the drop down list up. 在数据库中提供“ pic”列是否包含指向图像的链接是绝对的,这还取决于您如何设置下拉列表。

You will need an OnSelectedIndexChanged for the drop down which does the following: 您需要为下拉菜单执行以下操作:OnSelectedIndexChanged:

Selects the pic url from the database where subcategory = dropdown value or text. 从数据库中选择图片网址,其中子类别=下拉值或文本。 (this depends on how you set the drop-down list up). (这取决于您如何设置下拉列表)。

With that obtained value set the pic to the one you have obtained. 使用获得的值将图片设置为您获得的图片。

You need the following, providing code would make it easier. 您需要以下内容,提供代码将使其变得更容易。

In you ASP file add the following to the dropdown: 在您的ASP文件中,将以下内容添加到下拉列表中:

OnSelectedIndexChanged="drp_SelectedIndexChanged" AutoPostBack="True"

Within code behind you need: 在后面的代码中,您需要:

   protected void drp_SelectedIndexChanged(object sender, EventArgs e) {
        //SQL To obtain url for image based on drop down selection
        //providing code makes this easier
        //with return url:
      Image1.ImageUrl = //returned url
    }

Are you having issue in showing image in image control ? 在图像控件中显示图像时遇到问题吗? Can you right click on that container and check path in properties ? 您可以右键单击该容器并检查属性中的路径吗?

You can use AutoPostBack as True and "OnSelectedIndexChanged" to call server function and update image control based on result. 您可以将AutoPostBack用作True并使用“ OnSelectedIndexChanged”来调用服务器功能并根据结果更新图像控件。

Or you can use JQuery to show images in Div tag. 或者,您可以使用JQuery在Div标签中显示图像。

jQuery(document).ready(function(){
  $("#dropdownID").change(function() {
    $.ajax({
     url: "SericeFunction name",
     success: function(msg){
      .....<bind img tag >
     }
   });
  });
});

Thank You All of you guys especially @John for your syntax you provided thanks alot.. Below is the code : 谢谢大家,尤其是@John,非常感谢您提供的语法。.下面是代码:

protected void btnshow_Click(object sender, EventArgs e)
{
    using (var cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\PROJECT SEM6\Online Tours and Travels\App_Data\ToursandTravels.mdf;Integrated Security=True;User Instance=True"))
    using (var cmd = cn.CreateCommand())
    {
        cn.Open();
        cmd.CommandText = "select pic from subcategory where subcatid = '"+DropDownList1.Text+"'";
        cmd.Parameters.AddWithValue("@subcatid",DropDownList1.Text);
        using (var reader = cmd.ExecuteReader())
        {
            if (reader.Read())
            {
                var filePath = reader.GetString(0);
                // For this to work images must be stored inside the web application.
                // filePath must be a relative location inside the virtual directory
                // hosting the application. Depending on your environment some
                // transformations might be necessary on filePath before assigning it
                // to the image url.
                Image1.ImageUrl =("~/"+filePath);
            }
        }
    }   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM