DropDownList默认值的设定

方法:

(1)DropDownList1.SelectedValue=string;

(2)DropDownList1.Items.FindByText(string).Selected=true;

(3)DropDownList1.Items.FindByValue(string).Selected=true;


 昨天编程序的时候,才发现DropDownList是否绑定数据库和默认值的设定用的语句有影响,但是我没有弄明白是为什么。

绑定数据库的DropDownList:

eg:DropDownList1.SelectedValue=string; //string就是刚进页面时你要DropDownList所选的值。

非绑定数据库的DropDownList:

eg:DropDownList1.Items.FindByText(string).Selected=true; //string就是刚进页面时你要DropDownList所选的值。

用DropDownList1.Text的话,如果你的string不是第一个item,那么string就会冲掉第一个item,这样就导致第一个item没有了,而string就会出现两个。其他的方式,如用SelectedItem,或者绑定的和非绑定的用一样的语句,会造成各种各样的错误(有些情况是初始化的时候没错误,但是下面你取值提交的时候就会发生错误)。

 

进入页面之后,对DropDownList重新选Item,提交时,取DropDownList所选的Item时,绑定的和非绑定的就没有什么区别了,用DropDownList1.SelectedItem.Text就OK了~
### 如何使用 iTextPDF 进行 PDF 表单创建和编辑 #### 创建带有表单字段的 PDF 文档 为了创建一个新的包含表单字段的 PDF 文件,可以利用 `PdfWriter` 和 `AcroFields` 类来定义并设置这些字段。 ```java import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfReader; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.forms.PdfAcroForm; import com.itextpdf.forms.fields.PdfFormField; import com.itextpdf.forms.fields.PdfButtonFormField; import com.itextpdf.forms.fields.PdfChoiceField; import com.itextpdf.forms.fields.PdfTextField; public class CreatePDFForm { public static void main(String[] args) throws Exception { String dest = "form_created.pdf"; PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); try (pdfDoc) { // 添加文本框 PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true); PdfFormField field = PdfFormField.createText(pdfDoc, false).setName("name"); ((PdfTextField)field).setWidgetRect(new Rectangle(36, 708, 144, 12)).setMaxLength(50); form.addField(field); // 添加下拉列表框 List<String> options = Arrays.asList("Option 1", "Option 2", "Option 3"); PdfChoiceField dropDownList = PdfChoiceField.makeCombo(pdfDoc, null, options.toArray(new String[options.size()]), 0, new Rectangle(36, 690, 144, 12), false); dropDownList.setName("dropdownList"); form.addField(dropDownList.getAsFormField()); // 设置默认值和其他属性... form.getField("name").setValue("Default Name"); // 将 AcroForm 对象写入到文档中 form.flattenFields(); } } } ``` 这段代码展示了如何向空白页面添加简单的输入控件,比如文本框和组合框[^1]。 #### 编辑现有的 PDF 表单 对于已经存在的含有交互式表单的 PDF 文件,则可以通过读取该文件来进行修改: ```java String src = "existing_form.pdf"; String dest = "filled_form.pdf"; try (PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest))) { PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true); Map<String, PdfFormField> fields = form.getFormFields(); // 修改特定名称的字段内容 fields.get("text_field_name").setValue("New Value"); fields.get("checkbox_name").setValue(PdfCheckBoxFormField.CHECK_ON_VALUE); // 如果需要保存更改后的状态为静态视图而非可填写模式 form.flattenFields(); } catch (IOException e) { System.err.println(e.getMessage()); } ``` 此部分说明了怎样打开已有模板并对其中预设好的区域赋新值[^2]。 通过上述两段示例程序可以看出,在 Java 中借助于 iText 库能够轻松实现对 PDF 的动态管理——无论是新建还是更新既有资源都变得简单易行。值得注意的是实际项目里可能还需要考虑更多细节配置如字体样式调整、边界设定等额外参数以满足具体需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值