如何使用SQLDataSource将值检索到我的文本框中 [英] how to retrieve values to my text box, using SQLDataSource

查看:142
本文介绍了如何使用SQLDataSource将值检索到我的文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好:)

我有一个SQLData源,作为向导连接到列表框

选择项目时我想检索一些数据

假设这是我的桌子

Vedios:
-视频ID
-影片名称
-视频路径

我想(例如)在列表框中选择一个项目并将其设置为文本框中的文本时检索名称和路径.

我该怎么办?


谢谢大家:)

Hi all :)

I have a SQLData source, connected to listbox as a wizard

I want to retrieve some data when I choose an item

suppose here is my table

Vedios:
- Video ID
- Video Name
- Video Path

I want to ( for example ) to retrieve the name and the path when I choose an Item inside my listbox and set it as a text in atextbox

how can I do that ?


thank you all :)

推荐答案



这里''提供我的代码以在列表框中的选择更改时在文本框中显示数据

Hi,

Here ''m providing my code for showing data in textbox when selection changed in listbox

protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    MylocalDataClassesDataContext db = new MylocalDataClassesDataContext();
    var h = from g in db.VideoTabs
            where g.vid == int.Parse(ListBox1.SelectedValue)
            select g;
    foreach (VideoTab vt in h)
    {
        TextBox1.Text ="The Vid of selected Item is "+ vt.vid + " and Video Name is     " + vt.vname;
    }
}



在这里,我使用Linq从数据库中检索数据.

还有一件事是,您必须将ListBox控件的autopostback设置为true,默认情况下为false
最好.



Here I used Linq for retrieving data from database.

And onemore thing is you''ve to set autopostback to true for ListBox control by default it is false
All the Best.


我会尝试的. CreateData将是您的SQLDataSource
将视频路径设置为datavalue,将视频名称设置为datatextfield
确保下拉菜单中的自动回发设置为true,并在更改下拉选择时设置文本框文本.

I would try this. The CreateData would be your SQLDataSource
set the video path as the datavalue and the video name as the datatextfield
Be sure that you have auto postback set to true on the dropdown and set the text box text when the drop down selection is changed.

protected void  Page_Load(object sender, System.EventArgs e)
{
	if (!IsPostBack) {
		LoadDropDown();
	}

}


private List<VideosAndPaths> CreateVideoData()
{
	List<VideosAndPaths> listOfVideoAndPaths = new List<VideosAndPaths>();
	VideosAndPaths newVAP = default(VideosAndPaths);

	for (i = 0; i <= 10; i++) {
		newVAP = new VideosAndPaths();
		newVAP.VideoID = i;
		newVAP.VideoName = "Video #" + i.ToString;
		newVAP.VideoPath = "C:\\MyVideos\\" + newVAP.VideoName + ".avi";
		listOfVideoAndPaths.Add(newVAP);

	}

	return listOfVideoAndPaths;
}


private void LoadDropDown()
{
	this.ddlVideoList.DataTextField = "VideoName";
	this.ddlVideoList.DataValueField = "VideoPath";
	this.ddlVideoList.DataSource = CreateVideoData();
	this.ddlVideoList.DataBind();

	ListItem li = new ListItem("select Video", "-1");
	li.Selected = true;

	this.ddlVideoList.Items.Insert(1, li);

}


protected ddlVideoList_SelectedIndexChanged(object sender, EventArgs e)
{
	if (this.ddlVideoList.SelectedValue != "-1") {
		this.txtVideoPath.Text = this.ddlVideoList.SelectedValue;
	} else {
		this.txtVideoPath.Text = "";
	}
}


public class VideosAndPaths
{
	private int _videoID;
	public int VideoID {
		get { return _videoID; }
		set { _videoID = value; }
	}

	private string _videoName;
	public string VideoName {
		get { return _videoName; }
		set { _videoName = value; }
	}

	private string _videoPath;
	public string VideoPath {
		get { return _videoPath; }
		set { _videoPath = value; }
	}



}


这篇关于如何使用SQLDataSource将值检索到我的文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆