2011-07-03 22 views
1

我在ASP.Net WebForms中使用UpdatePanel上傳圖像。我將Image.ImageUrl設置爲圖像的完整虛擬路徑(/images/news/filenname.jpg),但我只想將文件名保存在數據庫中(filename.jpg)。如何自定義SqlDataSource以僅使用部分網址?自定義SqlDataSource以存儲部分字符串

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

回答

1

您可以設置的Page_Load和使用字符串函數的更新參數從完整的URL只得到文件名:

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