Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当用户在asp.net的dropdownlist中选择值时,如何在图像控件中显示存储在数据库中的图像路径?_C#_Asp.net_Sql Server_Image - Fatal编程技术网

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

C# 当用户在asp.net的dropdownlist中选择值时,如何在图像控件中显示存储在数据库中的图像路径?,c#,asp.net,sql-server,image,C#,Asp.net,Sql Server,Image,我有一个数据库,如下图所示: 以下是表格的图像: 在此表单中,我想在图像控件中显示图像,其中图像路径存储在数据库中,并且当用户选择它时,根据下拉列表中的子类别值,我可以如何操作请帮助。。。非常感谢。 伙计们,这是我表单的源代码,我已经添加了sqldatasource,并将下拉列表与sqldatasource绑定 <form method="post" enctype="multipart/form-data"> <table border="0" width="450px"

我有一个数据库,如下图所示:

以下是表格的图像: 在此表单中,我想在图像控件中显示图像,其中图像路径存储在数据库中,并且当用户选择它时,根据下拉列表中的子类别值,我可以如何操作请帮助。。。非常感谢。 伙计们,这是我表单的源代码,我已经添加了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>

更新子类别





如果数据库中的“pic”列包含指向图像的绝对链接,这还取决于您如何设置下拉列表

您需要为下拉列表更改OnSelectedIndexChanged,该下拉列表执行以下操作:

从数据库中选择pic url,其中subcategory=下拉值或文本。(这取决于您如何设置下拉列表)

使用获得的值,将pic设置为已获得的pic

您需要以下内容,提供代码将使其更容易

在ASP文件中,将以下内容添加到下拉列表中:

OnSelectedIndexChanged="drp_SelectedIndexChanged" AutoPostBack="True"
在代码背后,您需要:

   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
    }

在图像控制中显示图像是否有问题?您可以右键单击该容器并在属性中检查路径吗

您可以使用AutoPostBack作为True和“OnSelectedIndexChanged”调用服务器函数并根据结果更新图像控件

或者可以使用JQuery在Div标记中显示图像

jQuery(document).ready(function(){
  $("#dropdownID").change(function() {
    $.ajax({
     url: "SericeFunction name",
     success: function(msg){
      .....<bind img tag >
     }
   });
  });
});
jQuery(文档).ready(函数(){
$(“#dropdownID”).change(函数(){
$.ajax({
url:“服务功能名称”,
成功:功能(msg){
.....
}
});
});
});

谢谢大家,尤其是@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);
            }
        }
    }   

我们不知道如何在不查看原始代码的情况下对现有代码库进行更改。请发布,并充分解释需要修改的内容。请添加代码我已将我的下拉列表与SqlDataSource绑定在suubcategory列@Guanabantank you@John中感谢您的帮助您的代码的上述语法为我提供了如何实现它的基本思路非常感谢:)兄弟,我还有一个问题,你能告诉我如何使用文件上传控制更新数据库中图像的路径:)@John