리셋 없음 레벨 콤보 드롭다운 상자 (.net)
17148 단어 .net
프론트 데스크 코드:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title> </title>
<script type="text/javascript">
function CallServer(args,context)
{
context.innerHTML = "Loading...";
<%= ClientScript.GetCallbackEventReference(this,"args","GetServerData","context") %>
}
function GetServerData(result,context)
{
context.innerHTML = result;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="ddlCategory" runat="server"></asp:DropDownList>
<span id="pnlProduct">
<asp:DropDownList ID="ddlProduct" runat="server"></asp:DropDownList>
</span>
</div>
</form>
</body>
</html>
백그라운드 코드:
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Configuration;
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 _Default : System.Web.UI.Page,ICallbackEventHandler
{
protected string callbackResult;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindToCategory();
this.ddlCategory.Attributes.Add("onchange", "CallServer(this.value,pnlProduct)");
this.ddlProduct.Items.Add(new ListItem("-- --", "-1"));
}
}
/// <summary>
///
/// </summary>
private void BindToCategory()
{
SqlConnection conn = new SqlConnection("Server=(local);database=northwind;uid=sa;pwd=123");
SqlDataAdapter sda = new SqlDataAdapter("SELECT categoryID,categoryName FROM categories", conn);
DataTable dt = new DataTable();
sda.Fill(dt);
this.ddlCategory.DataSource = dt;
this.ddlCategory.DataTextField = "categoryName";
this.ddlCategory.DataValueField = "categoryID";
this.ddlCategory.DataBind();
this.ddlCategory.Items.Insert(0, new ListItem("-- --", "-1"));
}
private void BindToProduct(int categoryID)
{
SqlConnection conn = new SqlConnection("Server=(local);database=northwind;uid=sa;pwd=123");
SqlDataAdapter sda = new SqlDataAdapter("SELECT productID,productName FROM products WHERE categoryID=" + categoryID, conn);
DataTable dt = new DataTable();
sda.Fill(dt);
this.ddlProduct.DataSource = dt;
this.ddlProduct.DataTextField = "productName";
this.ddlProduct.DataValueField = "productID";
this.ddlProduct.DataBind();
if (ddlProduct.Items.Count == 0)
{
this.ddlProduct.Items.Add(new ListItem("-- --", "-1"));
}
}
private string RenderElement(Control control)
{
StringWriter writer = new StringWriter();
HtmlTextWriter output = new HtmlTextWriter(writer);
control.RenderControl(output);
output.Flush();
output.Close();
string htmlCode = writer.ToString();
writer.Close();
return htmlCode;
}
void ICallbackEventHandler.RaiseCallbackEvent(string str)
{
this.callbackResult = str;
}
string ICallbackEventHandler.GetCallbackResult()
{
int categoryID = Convert.ToInt32(callbackResult);
BindToProduct(categoryID);
callbackResult = RenderElement(this.ddlProduct);
return callbackResult;
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
.net 데이터베이스 조작 프레임워크 SqlSugar의 간단한 시작SqlSugar는 오래된 브랜드입니다.NET 데이터베이스 조작 프레임워크, 과당 빅데이터 과학기술팀이 유지 보수 및 업데이트, Github star 수 EF 및 Dapper 버금가 지원 데이터베이스: MySql, S...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.