【问题标题】:Customize SqlDataSource to store part of string自定义 SqlDataSource 以存储部分字符串
【发布时间】:2011-07-03 06:53:55
【问题描述】:

我在 ASP.Net WebForms 中使用 UpdatePanel 来上传图像。我将Image.ImageUrl 设置为图像的完整虚拟路径(/images/news/filenname.jpg),但我只想将文件名保存在数据库中(filename.jpg)。如何自定义 SqlDataSource 以仅占用 URL 的一部分?

<asp:SqlDataSource 
  ID="SqlDataSourceNews" 
  runat="server" 
  ConnectionString="<%$ ConnectionStrings:connectionString %>" 
  UpdateCommand="UPDATE [foo] SET [pic] = @pic WHERE [id] = @id">
   <UpdateParameters>
      <asp:Parameter Name="pic" Type="String" />
   </UpdateParameters>
   <InsertParameters>
      <asp:Parameter Name="pic" Type="String" />
    </InsertParameters>
 </asp:SqlDataSource>

【问题讨论】:

    标签: c# asp.net data-binding webforms


    【解决方案1】:

    您可以在 page_load 上设置更新参数并使用子字符串函数从完整 url 中获取文件名:

    protected void Buton_Click(object sender, EventArgs e)
    {
            SqlDataSource1.UpdateParameters["pic"].DefaultValue = "string fetched from substring method";
            SqlDataSource1.Update();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 2017-02-19
      相关资源
      最近更新 更多