HiBhavesh23,
You have to use HiddenField for saving the data.
Refer below sample.
HTML
Default
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Name:
</td>
<td>
<input type="text" id="txtName" readonly="readonly" />
</td>
</tr>
<tr>
<td></td>
<td>
<img id="imgPerson" runat="server" />
<asp:HiddenField ID="hfUrl" runat="server" />
</td>
</tr>
<tr>
<td></td>
<td>
<input type="button" value="Select Name" onclick="Select()" />
</td>
</tr>
</table>
<br />
<asp:Button Text="Save" runat="server" OnClick="OnSave" />
<script type="text/javascript">
var popup;
function Select() {
popup = window.open("Popup.aspx", "Popup", "width=200,height=200");
popup.focus();
}
function SetSource(src) {
document.getElementById("imgPerson").src = src;
document.getElementById("hfUrl").value = src;
}
</script>
Popup
<select name="ddlNames" id="ddlNames">
<option value="Mudassar Khan">Mudassar Khan</option>
<option value="John Hammond">John Hammond</option>
<option value="Mike Stanley">Mike Stanley</option>
</select>
<br />
<img id="img" src="Images/Penguins.jpg" />
<br />
<input type="button" value="Select" onclick="SetName();" />
<script type="text/javascript">
function SetName() {
if (window.opener != null && !window.opener.closed) {
var txtName = window.opener.document.getElementById("txtName");
var imgPerson = window.opener.document.getElementById("imgPerson");
txtName.value = document.getElementById("ddlNames").value;
window.opener.SetSource(document.getElementById("img").src);
}
window.close();
}
</script>
Code
Default
C#
protected void OnSave(object sender, EventArgs e)
{
string image = hfUrl.Value;
var webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData(image);
string MainDir = @"~/VisiterImage/";
string SubDir = DateTime.Now.ToString("dd-MM-yyyy");
if (!Directory.Exists(Server.MapPath(MainDir)))
{
Directory.CreateDirectory(Server.MapPath(MainDir));
}
if (!Directory.Exists(Server.MapPath(MainDir + SubDir)))
{
Directory.CreateDirectory(Server.MapPath(MainDir + SubDir));
}
string imageName = "Image.jpg";
File.WriteAllBytes(Path.Combine(Server.MapPath(MainDir + SubDir), imageName), imageBytes);
// Code to save the byte data imageBytes in database.
}
VB.Net
Protected Sub OnSave(ByVal sender As Object, ByVal e As EventArgs)
Dim image As String = hfUrl.Value
Dim webClient = New WebClient()
Dim imageBytes As Byte() = webClient.DownloadData(image)
Dim MainDir As String = "~/VisiterImage/"
Dim SubDir As String = DateTime.Now.ToString("dd-MM-yyyy")
If Not Directory.Exists(Server.MapPath(MainDir)) Then
Directory.CreateDirectory(Server.MapPath(MainDir))
End If
If Not Directory.Exists(Server.MapPath(MainDir & SubDir)) Then
Directory.CreateDirectory(Server.MapPath(MainDir & SubDir))
End If
Dim imageName As String = "Image.jpg"
File.WriteAllBytes(Path.Combine(Server.MapPath(MainDir & SubDir), imageName), imageBytes)
' Code to save the byte data imageBytes in database.
End Sub