ورود

View Full Version : مبتدی: نشان دادن اطلاعات گرید در تکست باکس



vahidarjmand
دوشنبه 13 اردیبهشت 1389, 00:10 صبح
با سلام
من یک gridview دارم که اطلاعاتشو از dataset میگیرد .حالا میخواهم با select کردن سطر مورد نظر از grid فیلد های مورد نظر از grid در textbox نمایش داده شود
لطفا کمک کنید

با تشکر

vahidarjmand
دوشنبه 13 اردیبهشت 1389, 09:11 صبح
کسی نظری نداره!
کمک کنید

maxpayn2
دوشنبه 13 اردیبهشت 1389, 10:28 صبح
سلام

نمونه کد aspx :




<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
&nbsp;<asp:Label ID="lblMessage" runat="server" ForeColor="Red" Text="Label" Visible="False"></asp:Label><br />

&nbsp;<asp:ValidationSummary ID="ValidationSummary1" runat="server" DisplayMode="List" ValidationGroup="1" />
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand"
DataSourceID="SqlDataSource1" Width="400"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnselect" runat="server" Text="انتخاب" CommandArgument='<%#Eval("ClerckID")%>' CommandName="Select" CausesValidation="false" />
</ItemTemplate>
<ItemStyle Font-Names="Tahoma" Width="50" HorizontalAlign="Center" />
<HeaderStyle Font-Names="Tahoma" Width="50" HorizontalAlign="Center" />
</asp:TemplateField>
<asp:TemplateField HeaderText="متن خبر">
<ItemTemplate>
&nbsp;<%#Eval("titr")%>
</ItemTemplate>
<ItemStyle Font-Names="Tahoma" />
<HeaderStyle Font-Names="Tahoma" />
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<asp:Label ID="Label2" runat="server" Text="در حال حاضر خبری برای ویرایش موجود نیست"></asp:Label>
</EmptyDataTemplate>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:tconetConnectionString %>" SelectCommand="SELECT * FROM [news]"></asp:SqlDataSource><br />
<table>
<tr>
<td style="text-align: right">&nbsp;<asp:Label ID="Label4" runat="server" Text="عکس خبر :"></asp:Label></td>
<td><asp:FileUpload ID="newsImageFileUpload" runat="server" Width="266px" TabIndex="1" /></td>
<td>&nbsp;<asp:Label ID="Label5" runat="server" Font-Size="XX-Small" ForeColor="DimGray" Text="عرض = 90 پیکسل ، حداکثر حجم = 50 کیلوبایت"></asp:Label><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="لطفا یک عکس کوچک برای خبر انتخاب نمایید" ControlToValidate="newsImageFileUpload" ValidationGroup="1">*</asp:RequiredFieldValidator></td>
</tr>
<tr>
<td style="text-align: right">&nbsp;<asp:Label ID="Label1" runat="server" Text="تیتر خبر :" CssClass="aligne_right"></asp:Label></td>
<td><asp:TextBox ID="newsTitrTextBox" runat="server" Width="262px" CssClass="aligne_right" TabIndex="2"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="لطفا تیتر خبر را وارد نمایید" ControlToValidate="newsTitrTextBox" ValidationGroup="1">*</asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>&nbsp;<asp:Label ID="Label2" runat="server" Text="خلاصه خبر :"></asp:Label></td>
<td style="text-align: right"><asp:TextBox ID="newsSubTextBox" runat="server" Width="262px" CssClass="aligne_right" TabIndex="3"></asp:TextBox></td>
<td><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="newsSubTextBox" ErrorMessage="لطفا خلاصه خبر را وارد نمایید" ValidationGroup="1">*</asp:RequiredFieldValidator></td>
</tr>
<tr>
<td>&nbsp;<asp:Label ID="Label6" runat="server" Text="فعال :"></asp:Label></td>
<td><asp:CheckBox ID="CheckBox1" runat="server" TabIndex="4" /></td>
<td></td>
</tr>
<tr>
<td colspan="3" style="text-align: right">&nbsp;<asp:Label ID="Label3" runat="server" Text="متن خبر :"></asp:Label></td>
</tr>
</table>
<FCKeditorV2:FCKeditor ID="FCKeditor1" runat="server" BasePath="~/FCKeditor/" Height="400" EditorAreaCSS="aligne_right">
</FCKeditorV2:FCKeditor><br />
&nbsp;<asp:Button ID="save" runat="server" Text="ذخیره" Width="50px" OnClick="save_Click" ValidationGroup="1" Font-Names="Tahoma" TabIndex="5" /><br /><br />
</asp:Content>



نمونه کد C# :




using System;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Admin_EditNews : System.Web.UI.Page
{
int faal, ImageID;
bool role, imageExis, fileExt, fileType, fileLength, fileWidth, fck = false;
string path, pathfull, pathh, uploadErrorMessage;
int fileMaxLength = System.Convert.ToInt32(System.Configuration.Config urationManager.AppSettings["ImageMaxFileLengh"]);
int fileMaxWidth = System.Convert.ToInt32(System.Configuration.Config urationManager.AppSettings["ImageMaxWidth"]);
string domainURL = System.Configuration.ConfigurationManager.AppSetti ngs["domainURL"];

protected void Page_Load(object sender, EventArgs e)
{
Session["UrlReferrer"] = "~/Admin/EditNews.aspx";

if (Session["role"].ToString() == "" || Session["role"].ToString() == null)
{
Server.Transfer("~/SessionTimeOut.aspx");
}
if (Session["role"].ToString() == "admin" || Session["role"].ToString() == "news")
{
role = true;
}
else
{
role = false;
Server.Transfer("~/SessionTimeOut.aspx");
}
}
protected void save_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection();
SqlConnection cnn1 = new SqlConnection();
SqlConnection cnn2 = new SqlConnection();

try
{
if (role == true)
{
if (FCKeditor1.Value == null || FCKeditor1.Value == "")
{
lblMessage.Text = "لطفا متن خبر را وارد کنید";
lblMessage.Visible = true;
}
else
{
fck = true;
}

if (fck == true)
{
Session["imagename"] = newsImageFileUpload.FileName;
String fileExtension = System.IO.Path.GetExtension(newsImageFileUpload.Fi leName).ToLower();
String[] allowedExtensions = { ".jpeg", ".jpg" };
System.Drawing.Bitmap oBitmap = new System.Drawing.Bitmap(newsImageFileUpload.PostedFi le.InputStream, true);
path = Server.MapPath("~/Pictures");

for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileExt = true;
}
else
{
uploadErrorMessage = "فرمت فایل " + "jpg" + "یا" + "jpeg" + " نبوده و ارسال نشد";
lblMessage.Text = uploadErrorMessage;
lblMessage.Visible = true;
fileExt = false;
}
}

if (newsImageFileUpload.PostedFile.ContentType == "image/pjpeg")
{
fileType = true;
}
else
{
uploadErrorMessage = "فایل عکس نبوده و ارسال نشد";
lblMessage.Text = uploadErrorMessage;
lblMessage.Visible = true;
fileType = false;
}

if (newsImageFileUpload.PostedFile.ContentLength <= fileMaxLength * 1024)
{
fileLength = true;
}
else
{
uploadErrorMessage = "سایز عکس بیشتر از 50 کیلو بایت بوده و ارسال نشد";
lblMessage.Text = uploadErrorMessage;
lblMessage.Visible = true;
fileLength = false;
}

if (oBitmap.Width <= fileMaxWidth)
{
fileWidth = true;
}
else
{
uploadErrorMessage = "عرض عکس بیشتر از 300 پیکسل بوده و ارسال نشد";
lblMessage.Text = uploadErrorMessage;
lblMessage.Visible = true;
fileWidth = false;
}

try
{
cnn.ConnectionString = "Data Source=.;Database=tconet2;Integrated Security=SSPI";
cnn.Open();
SqlCommand cmd = new SqlCommand("select_imagename", cnn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@name", Session["imagename"].ToString());

SqlDataReader dr = cmd.ExecuteReader();

if (dr.Read())
{
lblMessage.Text = "قبلا یک عکس با این نام ثبت شده است ، لطفا نام عکس را تغییر دهید";
lblMessage.Visible = true;
imageExis = false;
}
else
{
imageExis = true;
}

cnn.Close();
}
finally
{
cnn.Close();
}

if (imageExis == true && fileExt == true && fileType == true && fileLength == true && fileWidth == true)
{

pathfull = Server.MapPath("~/Pictures") + "\\" + Session["imagename"].ToString() + "";
pathh = pathfull.Trim();
newsImageFileUpload.PostedFile.SaveAs(pathh);

string path2 = Server.MapPath("~/Id") + "\\ImageID.txt";
string pathh2 = path2.Trim();
StreamReader strm2 = new StreamReader(pathh2);
string str2 = strm2.ReadLine();
ImageID = Convert.ToInt32(str2) + 1;
strm2.Close();


string imgpath = domainURL + "/Pictures/" + Session["imagename"].ToString();

if (CheckBox1.Checked == true)
{
faal = 1;
}
else
{
faal = 0;
}

try
{
cnn1.ConnectionString = "Data Source=.;Database=tconet2;Integrated Security=SSPI";
cnn1.Open();
SqlCommand cmd1 = new SqlCommand("update_news", cnn1);
cmd1.CommandType = CommandType.StoredProcedure;

cmd1.Parameters.AddWithValue("@titr", newsTitrTextBox.Text);
cmd1.Parameters.AddWithValue("@sub_comment", newsSubTextBox.Text);
cmd1.Parameters.AddWithValue("@comment", FCKeditor1.Value);
cmd1.Parameters.AddWithValue("@image", imgpath);
cmd1.Parameters.AddWithValue("@allowed", faal);
cmd1.Parameters.AddWithValue("@id", Convert.ToInt32(Session["id"].ToString()));

cmd1.ExecuteNonQuery();

cnn1.Close();
}
finally
{
cnn1.Close();
}

try
{
cnn2.ConnectionString = "Data Source=.;Database=tconet2;Integrated Security=SSPI";
cnn2.Open();
SqlCommand cmd2 = new SqlCommand("insert_images", cnn2);
cmd2.CommandType = CommandType.StoredProcedure;

cmd2.Parameters.AddWithValue("@image_id", Convert.ToInt32(ImageID));
cmd2.Parameters.AddWithValue("@image_name", Session["imagename"].ToString());
cmd2.Parameters.AddWithValue("@image_address", imgpath);

cmd2.ExecuteNonQuery();

cnn2.Close();
}
finally
{
cnn2.Close();
}

StreamWriter swrm2 = new StreamWriter(pathh2);
swrm2.WriteLine(ImageID);
swrm2.Close();

lblMessage.Text = "خبر مورد نظر با موفقیت تغییر کرد";
lblMessage.Visible = true;
}
}

GridView1.DataBind();
}
}
catch (Exception ex)
{
Server.Transfer("~/ExceptionReport.aspx?id=" + ex.Message.ToString() + "&urle=" + Request.Url.ToString());
}
finally
{
cnn.Close();
cnn1.Close();
cnn2.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{

}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
Session["id"] = Convert.ToInt32(e.CommandArgument.ToString());

}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection cnn1 = new SqlConnection();

try
{
try
{
cnn1.ConnectionString = "Data Source=.;Database=tconet2;Integrated Security=SSPI";
cnn1.Open();
SqlCommand cmd1 = new SqlCommand("select_news", cnn1);
cmd1.CommandType = CommandType.StoredProcedure;

cmd1.Parameters.AddWithValue("@id", Convert.ToInt32(Session["id"].ToString()));

SqlDataReader dr1 = cmd1.ExecuteReader();

newsTitrTextBox.Text = "";
newsSubTextBox.Text = "";
newsTitrTextBox.Text = "";
FCKeditor1.Value.Clone();

if (dr1.Read())
{
newsTitrTextBox.Text = dr1[2].ToString();
newsSubTextBox.Text = dr1[3].ToString();
FCKeditor1.Value = dr1[4].ToString();
faal = Convert.ToInt32(dr1[9].ToString());
}

cnn1.Close();
}
finally
{
cnn1.Close();
}

if (faal == 1)
{
CheckBox1.Checked = true;
}
if (faal == 0)
{
CheckBox1.Checked = false;
}
}
catch (Exception ex)
{
Server.Transfer("~/ExceptionReport.aspx?id=" + ex.Message.ToString() + "&urle=" + Request.Url.ToString());
}
finally
{
cnn1.Close();
}
}
}




این کد رو خیلی وقت پیش نوشتم ، یادم نیست که تمام خط هاش درست کار میکنه یا نه ، ولی میتونی ازش ایده بگیری

vahidarjmand
دوشنبه 13 اردیبهشت 1389, 13:08 عصر
من فکر میکنم باید اینطوری باشه


Dim ds As New DataSet1.register_employeeDataTable
Dim dta As New DataSet1TableAdapters.register_employeeTableAdapte r
ds = dta.GetDataBy5(GridView2.DataKeys(e.NewEditIndex). Value)

TextBox4.Text = ds.Rows(0).Item("employee_no")

ولی از این خط error میگیره


ds = dta.GetDataBy5(GridView2.DataKeys(e.NewEditIndex). Value)